Skip to content

[release/10.0] Allow undisposed async file and pipe handles to be finalized on Windows - #137643

Open
adamsitnik wants to merge 3 commits into
dotnet:release/10.0from
adamsitnik:backport-137501-release-10.0
Open

adamsitnik wants to merge 3 commits into
dotnet:release/10.0from
adamsitnik:backport-137501-release-10.0

Conversation

@adamsitnik

@adamsitnik adamsitnik commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

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 by FileStream and RandomAccess) and async PipeStream instances 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 its PreAllocatedOverlapped.

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 FileStream with useAsync: true, RandomAccess on an async handle, and NamedPipeServerStream/NamedPipeClientStream with PipeOptions.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.

adamsitnik and others added 2 commits November 16, 2026 18:12
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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO Servicing-consider Issue for next servicing release review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants