-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add File.OpenNullHandle() for efficient process I/O redirection #123483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please address my feedback
src/libraries/System.Private.CoreLib/src/System/IO/File.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Show resolved
Hide resolved
…ests Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-io |
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
adamsitnik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new File.OpenNullHandle() API to efficiently obtain a handle to the system's null device (NUL on Windows, /dev/null on Unix). This enables efficient process I/O redirection by discarding output or providing empty input without the overhead of reading and ignoring data.
Changes:
- Added new public API
File.OpenNullHandle()to theSystem.IO.Fileclass - Implemented platform-specific core methods that open the null device with read/write access and non-inheritable handles
- Added comprehensive test coverage with 14 test cases covering sync/async read/write operations
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Runtime/ref/System.Runtime.cs | Added public API signature for OpenNullHandle() method |
| src/libraries/System.Private.CoreLib/src/System/IO/File.cs | Added public OpenNullHandle() method with comprehensive XML documentation |
| src/libraries/System.Private.CoreLib/src/System/IO/File.Windows.cs | Implemented Windows-specific core that opens "NUL" device with non-inheritable handle |
| src/libraries/System.Private.CoreLib/src/System/IO/File.Unix.cs | Implemented Unix-specific core that opens "/dev/null" device with O_CLOEXEC (non-inheritable) |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj | Added test file reference to project |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs | Added comprehensive tests for sync/async read/write operations and concurrent access |
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.Unix.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
/ba-g infrastructure timeouts |
Description
Adds
File.OpenNullHandle()API to obtain a handle to the system's null device (NULon Windows,/dev/nullon Unix). This enables efficient discarding of process output/error streams or providing empty input without the overhead of reading and ignoring data.Current inefficient pattern:
New API:
Changes
File.OpenNullHandle()toSystem.IO.FileNullDevicePathconstants inFile.Windows.cs("NUL") andFile.Unix.cs("/dev/null")File.cscallsSafeFileHandle.Open()with the platform-specific path constantFileOptions.None(enforcesO_CLOEXECon Unix for non-inheritability)O_CLOEXEC)FileStream, including:FileStream(handle, FileAccess.*)constructorsisAsyncparameter was attempted but reverted due to FileStream validation preventing use of sync handles withisAsync: true(tracked separately in issue Remove isAsync flag validation fromFileStreamctors #123760)Testing
All tests passing. Verified:
FileStream(handle, FileAccess.*)constructorsOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.