Tighten LimitArrayPoolWriteStream cleanup contract#128328
Open
Copilot wants to merge 5 commits into
Open
Conversation
…fers directly Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/67c929c4-11bf-4536-bd71-7f640ddcfb01 Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…try/finally Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/67c929c4-11bf-4536-bd71-7f640ddcfb01 Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…ionException Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/648b000d-d351-4916-bf8c-13142b1ebff1 Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/419e5e57-fc2e-45ea-94ed-3a3b584e22dd Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…PoolWriteStream Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/2bc5c1de-f6bd-40ff-9f88-efbc27d10f7e Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
MihaZupan
May 18, 2026 14:31
View session
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
MihaZupan
approved these changes
May 18, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Tightens the cleanup contract for the internal LimitArrayPoolWriteStream used by HttpContent buffering. Previously Dispose quietly returned pooled buffers, which meant a user-supplied SerializeToStream(Async) override that disposed the passed-in stream could silently corrupt the buffered content / ArrayPool.Shared. After this PR, Dispose is reserved for "the user did something wrong" and throws, while all framework-side cleanup goes through the new internal ReturnAllPooledBuffers() directly.
Changes:
LimitArrayPoolWriteStream.Dispose(bool)now throwsInvalidOperationException(with explanatory comment);ReturnAllPooledBuffersis promoted fromprivatetointernal.- All framework callsites (
HttpContent.LoadIntoBuffer[Async],LoadIntoBufferAsyncCore,HttpContent.Dispose,HttpClient.GetStringAsyncCore/GetByteArrayAsyncCore) replaceDispose()/usingwith explicitReturnAllPooledBuffers()calls infinally/catch. - New unit test verifies that disposing the buffered stream from a user
SerializeToStreamAsyncoverride surfacesInvalidOperationExceptionout ofLoadIntoBufferAsync;StreamToStreamCopyTestcleanup is simplified to best-effort.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs | Switches internal cleanup paths to ReturnAllPooledBuffers, exposes it internal, and makes Dispose throw to catch user misuse. |
| src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs | Replaces using var buffer = ... patterns in GetStringAsyncCore / GetByteArrayAsyncCore with nullable locals and explicit ReturnAllPooledBuffers() in finally. |
| src/libraries/System.Net.Http/tests/UnitTests/HttpContentTest.cs | Adds regression test asserting InvalidOperationException when a user SerializeToStreamAsync disposes the buffered stream. |
| src/libraries/System.Net.Http/tests/UnitTests/StreamToStreamCopyTest.cs | Drops using on LimitArrayPoolWriteStream instances (now invalid) and inserts best-effort ReturnAllPooledBuffers() calls in the resizing test. |
This was referenced May 18, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make
LimitArrayPoolWriteStream.Disposeunconditionally throw.This is an internal type where we always control the lifetime. The user should never be disposing it.
See #128083 (comment)