Skip to content

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

Open
adamsitnik wants to merge 2 commits into
dotnet:release/11.0from
adamsitnik:backport-137501-release-11.0
Open

adamsitnik wants to merge 2 commits into
dotnet:release/11.0from
adamsitnik:backport-137501-release-11.0

Conversation

@adamsitnik

Copy link
Copy Markdown
Member

Backport of #137507 to release/11.0

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.

… on Windows (dotnet#137507)

Resolves dotnet#137501

---------

Copilot-Session: 8cfdacb8-9380-470b-83d0-2598d9255b9b
(cherry picked from commit e863295)
@adamsitnik adamsitnik added this to the 11.0.0 milestone Nov 16, 2026
@adamsitnik adamsitnik added Servicing-consider Issue for next servicing release review area-System.IO labels Nov 16, 2026
@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 <223556219+Copilot@users.noreply.github.com>
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-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants