-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
base: main
Are you sure you want to change the base?
Conversation
The main issue is that the StreamWriter would close the stream causing an exeption when the wed to read it.
There was a problem hiding this comment.
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())) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
} | ||
static async Task<IRandomAccessStream> CopyContentToRandomAccessStreamAsync(Stream content) | ||
{ | ||
var ras = new InMemoryRandomAccessStream(); |
There was a problem hiding this comment.
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;
}
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