Repository navigation
[release/10.0] Allow undisposed async file and pipe handles to be finalized on Windows - #137643
Open
adamsitnik wants to merge 3 commits into
Open
adamsitnik wants to merge 3 commits into
adamsitnik wants to merge 3 commits into
Conversation
Backport of the SafeFileHandle part of dotnet#137507. The cached OverlappedValueTaskSource is rooted by its PreAllocatedOverlapped, so it must not reference the handle or the cancellation token source of the last operation. The source is also returned for reuse only when GetResult consumed the result of a completed operation. Co-authored-by: Copilot <[email protected]> Copilot-Session: 8cfdacb8-9380-470b-83d0-2598d9255b9b
Backport of the System.IO.Pipes part of dotnet#137507. The cached PipeValueTaskSource instances are rooted by their PreAllocatedOverlapped, so they must not reference the stream, the cancellation token source or the ExecutionContext of the last operation. They are also disposed when the stream is finalized, and returned for reuse only when GetResult consumed the result of a completed operation. Co-authored-by: Copilot <[email protected]> Copilot-Session: 8cfdacb8-9380-470b-83d0-2598d9255b9b
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
jozkee
approved these changes
Nov 16, 2026
The tests checked GC reachability right after awaiting an async helper. The helper's continuation references the streams (or the process) until the thread that completed it is done with it, which raced with the GC in the test and made the tests flaky. Use synchronous helpers instead. Co-authored-by: Copilot <[email protected]> Copilot-Session: 8cfdacb8-9380-470b-83d0-2598d9255b9b
This was referenced Nov 17, 2026
This branch has not been deployed
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.
Backport of #137507 to release/10.0
The product changes are same, the tests differ as some helper APIs were introduced in .NET 11.
Description
On Windows, async
SafeFileHandle(used byFileStreamandRandomAccess) and asyncPipeStreaminstances can't be finalized after an async read or write unless they're disposed. Each one rents a reusable value-task source and caches it on the handle or stream for the next operation. The cached source was rooted by itsPreAllocatedOverlapped.Customer Impact
Windows apps that open files or pipes for async I/O and don't dispose them leak one OS handle per object. Examples are
FileStreamwithuseAsync: true,RandomAccesson an async handle, andNamedPipeServerStream/NamedPipeClientStreamwithPipeOptions.Asynchronous. For named pipes, the pipe instance itself stays open. The handles are released only when the process exits, so long-running services that don't dispose these objects accumulate them.Regression
No. The issue has existed since .NET 6 (#51363, #53669).
Testing
New tests were added, all of which fail without the fix.
Risk
Low. The changes are limited to the reusable value-task sources used on Windows for async file and pipe I/O. The sources are still reused (no performance regression), but they reference the safe handle only when they are being used.