Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HybridWebView] Properly managed response streams #28230

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mattleibow
Copy link
Member

Description of Change

The main issue that this PR fixes is the case where we write to a stream using a StreamWriter, which is then disposed causing the stream to be disposed as well.

Instead of just leaving the stream open, I refactored the writing code to not copy so many times. Before we would copy from the source stream into a MemoryStream and then into a InMemoryRandomAccessStream. I now use the extension method to wrap the InMemoryRandomAccessStream in a Stream and write directly.

I also cache the 404 message and write the bytes directly instead of first converting/copying to a stream.

Issues Fixed

Fixes #27953

The main issue is that the StreamWriter would close the stream
causing an exeption when the wed to read it.
@Copilot Copilot bot review requested due to automatic review settings March 6, 2025 16:45
@mattleibow mattleibow requested a review from a team as a code owner March 6, 2025 16:45
@mattleibow mattleibow added this to the .NET 9 SR5 milestone Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR refactors the response stream handling in HybridWebView to reduce unnecessary stream copies and ensure the 404 response is managed more efficiently while adding a relevant test.

  • Refactored stream handling in Windows-specific HybridWebView handler to use IBuffer directly instead of multiple MemoryStream copies.
  • Introduced a lazy-loaded cached 404 message buffer to optimize error responses.
  • Added new test cases in HybridWebViewTests to check file request statuses via JavaScript.

Reviewed Changes

File Description
src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs Added new test "RequestFileFromJS" with InlineData to validate response statuses.
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs Refactored stream copying methods and added a lazy-loaded 404 message buffer to streamline response handling.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

@@ -174,24 +175,28 @@ private async void OnWebResourceRequested(CoreWebView2 sender, CoreWebView2WebRe
}

// 3.b. Otherwise, return a 404
var ras404 = new InMemoryRandomAccessStream();
using (var writer = new StreamWriter(ras404.AsStreamForWrite()))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the main issue. The writer will also dispose the stream.

@PureWeen PureWeen added the p/0 Work that we can't release without label Mar 6, 2025
@mattleibow mattleibow requested a review from tj-devel709 March 6, 2025 17:37
Copy link
Member

@tj-devel709 tj-devel709 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jsuarezruiz jsuarezruiz added the area-blazor Blazor Hybrid / Desktop, BlazorWebView label Mar 7, 2025
}
static async Task<IRandomAccessStream> CopyContentToRandomAccessStreamAsync(Stream content)
{
var ras = new InMemoryRandomAccessStream();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both methods have similar logic for creating an InMemoryRandomAccessStream, could we refactor it creating a helper method?

private static async Task<IRandomAccessStream> CreateRandomAccessStreamAsync(Func<IRandomAccessStream, Task> writeContentAsync)
{
	var ras = new InMemoryRandomAccessStream();
	await writeContentAsync(ras);
	await ras.FlushAsync();
	ras.Seek(0);
	return ras;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView p/0 Work that we can't release without
Projects
Status: Approved
Development

Successfully merging this pull request may close these issues.

HybridWebView crashing every time in Windows now with 9.0.40 update
4 participants