Implement async handle support on Unix for FileStream#125377
Draft
Copilot wants to merge 3 commits intocopilot/extend-safe-file-handle-pipefrom
Draft
Implement async handle support on Unix for FileStream#125377Copilot wants to merge 3 commits intocopilot/extend-safe-file-handle-pipefrom
Copilot wants to merge 3 commits intocopilot/extend-safe-file-handle-pipefrom
Conversation
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement async handle support on Unix for FileStream
Implement async handle support on Unix for FileStream
Mar 10, 2026
Member
|
@stephentoub regarding #125220 (comment) this is what it would take to get |
adamsitnik
reviewed
Mar 10, 2026
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenHandle.cs
Outdated
Show resolved
Hide resolved
Member
|
If we move all of the async infrastructure from System.Net.Sockets into ThreadPool (support for synchronously and asynchronously waiting on arbitrary file descriptors), this would come "for free", right? |
Member
Then the synchronous implementation remains the same, but the asynchronous gains cancellation support and improved throughput (we use one global thread for polling rather than one thread per each read). |
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.
Description
FileStream previously rejected non-blocking (
O_NONBLOCK) handles on Unix. This PR lifts that restriction by adding proper poll-then-read/write support for non-blocking file descriptors, enablingFileStreamto wrap async pipe/socket handles on Unix.Native layer (
pal_io.c,pal_io.h,entrypoints.c)SystemNative_ReadFromNonblockingandSystemNative_WriteToNonblockingEAGAIN/EWOULDBLOCK, polls (indefinitely, on a thread pool thread) viaCommon_Polluntil data/space is available or the pipe is closed, then retries the I/OEPIPEInterop (
Interop.Read.cs,Interop.Write.cs)ReadFromNonblockingandWriteToNonblockingP/Invoke declarationsRandomAccess.Unix.cs
ReadAtOffsetandWriteAtOffsetnow checkhandle.IsAsyncfirst and dispatch to the new non-blocking variants; the existingSupportsRandomAccess/pread/pwritepath is unchanged for synchronous handlesFileStream / tests
ValidateHandleguard that threwArgumentExceptionfor async handles on UnixAsyncHandleOnUnix_FileStream_ctor_Throwstest withSafeFileHandle_CreateAnonymousPipe_FileStream_SetsIsAsyncAndTransfersData, which verifies all fourasyncRead/asyncWritecombinations work end-to-end usingFileStreamdirectly (noAnonymousPipeClientStreamshim needed)Changes
src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cssrc/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Unix.cssrc/libraries/Common/src/Interop/Unix/System.Native/Interop.Read.cssrc/libraries/Common/src/Interop/Unix/System.Native/Interop.Write.cssrc/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenHandle.cssrc/native/libs/System.Native/pal_io.csrc/native/libs/System.Native/pal_io.hsrc/native/libs/System.Native/entrypoints.c💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.