Add File.OpenNullHandle() for efficient process I/O redirection#123483
Merged
adamsitnik merged 7 commits intomainfrom Jan 30, 2026
Merged
Add File.OpenNullHandle() for efficient process I/O redirection#123483adamsitnik merged 7 commits intomainfrom
adamsitnik merged 7 commits intomainfrom
Conversation
Co-authored-by: adamsitnik <[email protected]>
Co-authored-by: adamsitnik <[email protected]>
Copilot
AI
changed the title
[WIP] Add low-level APIs for opening null device
Add File.OpenNullHandle() for efficient process I/O redirection
Jan 22, 2026
adamsitnik
reviewed
Jan 22, 2026
Member
adamsitnik
left a comment
There was a problem hiding this comment.
@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 <[email protected]>
stephentoub
reviewed
Jan 22, 2026
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Show resolved
Hide resolved
Contributor
Author
Co-authored-by: stephentoub <[email protected]>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
adamsitnik
reviewed
Jan 29, 2026
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/OpenNullHandle.cs
Outdated
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment.
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
stephentoub
reviewed
Jan 29, 2026
src/libraries/System.Private.CoreLib/src/System/IO/File.Unix.cs
Outdated
Show resolved
Hide resolved
stephentoub
approved these changes
Jan 29, 2026
Co-authored-by: adamsitnik <[email protected]>
Member
|
/ba-g infrastructure timeouts |
This was referenced Jan 30, 2026
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
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.