-
Notifications
You must be signed in to change notification settings - Fork 6.1k
docs: Breaking change — SafeFileHandle.IsAsync and FileStream.IsAsync now reflect actual O_NONBLOCK state on Unix (.NET 11) #52888
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
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/fix-safe-file-handle-is-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c00709
Initial plan
Copilot 743265b
Add breaking change article for SafeFileHandle.IsAsync Unix behavior …
Copilot b90f9cb
Fix SafeFileHandle.IsAsync behavior on Unix
gewarren 2a1f4e0
Clarify Previous behavior section: cover pipe/socket O_NONBLOCK case
Copilot 5efbcd9
Update documentation for SafeFileHandle.IsAsync behavior
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
docs/core/compatibility/core-libraries/11/safefilehandle-isasync-unix.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| title: "Breaking change: SafeFileHandle.IsAsync and FileStream.IsAsync accurately reflect non-blocking state on Unix" | ||
| description: "Learn about the breaking change in .NET 11 where SafeFileHandle.IsAsync and FileStream.IsAsync on Unix now accurately reflect whether the underlying file descriptor is in non-blocking mode." | ||
| ms.date: 04/07/2026 | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # SafeFileHandle.IsAsync and FileStream.IsAsync accurately reflect non-blocking state on Unix | ||
|
|
||
| On Unix, <xref:Microsoft.Win32.SafeHandles.SafeFileHandle.IsAsync?displayProperty=nameWithType> and <xref:System.IO.FileStream.IsAsync?displayProperty=nameWithType> now accurately reflect whether the underlying file descriptor has the `O_NONBLOCK` flag set. Previously, `IsAsync` unconditionally returned `true` for regular files opened with <xref:System.IO.FileOptions.Asynchronous?displayProperty=nameWithType>, even though regular file I/O on Unix is inherently synchronous at the kernel level. | ||
|
|
||
| ## Version introduced | ||
|
|
||
| .NET 11 Preview 3 | ||
|
|
||
| ## Previous behavior | ||
|
|
||
| Previously, `SafeFileHandle.IsAsync` on Unix returned `true` for regular files opened with `FileOptions.Asynchronous`, even though no `O_NONBLOCK` flag was set on the file descriptor (regular file I/O on Unix is inherently synchronous). For file descriptors that were genuinely non-blocking `IsAsync` incorrectly returned `false` on Unix. | ||
|
|
||
| ```csharp | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| // On Unix, IsAsync was true for regular files opened with FileOptions.Asynchronous, | ||
| // even though the file descriptor had no O_NONBLOCK set. | ||
| using SafeFileHandle handle = File.OpenHandle("myfile.txt", options: FileOptions.Asynchronous); | ||
| Console.WriteLine(handle.IsAsync); // true (misleading; no O_NONBLOCK on regular file) | ||
| ``` | ||
|
|
||
| On non-Windows platforms, constructing a `SendPacketsElement` with a non-async `FileStream` threw an `ArgumentException`. | ||
|
|
||
| ## New behavior | ||
|
|
||
| Starting in .NET 11, `SafeFileHandle.IsAsync` on Unix returns `true` only when the underlying file descriptor has the `O_NONBLOCK` flag set, which is possible for pipes and sockets. For regular files, it returns `false`. | ||
|
|
||
| ```csharp | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| // On Unix, IsAsync now reflects the actual | ||
| // non-blocking state of the file descriptor. | ||
| SafeFileHandle.CreateAnonymousPipe( | ||
| out SafeFileHandle readHandle, | ||
| out SafeFileHandle writeHandle, | ||
| asyncRead: true, | ||
| asyncWrite: false); | ||
|
|
||
| Console.WriteLine(readHandle.IsAsync); // true (O_NONBLOCK set on read end) | ||
| Console.WriteLine(writeHandle.IsAsync); // false (blocking write end) | ||
| ``` | ||
|
|
||
| For regular files opened with `FileOptions.Asynchronous`, `IsAsync` correctly returns `false` on Unix because regular file I/O is inherently synchronous at the kernel level. | ||
|
|
||
| Additionally, on non-Windows platforms, constructing a `SendPacketsElement` with a `FileStream` no longer throws `ArgumentException` regardless of whether the stream is async. | ||
|
|
||
| ## Type of breaking change | ||
|
|
||
| This change is a [behavioral change](../../categories.md#behavioral-change). | ||
|
|
||
| ## Reason for change | ||
|
|
||
| The previous behavior was incorrect and misleading. `SafeFileHandle.IsAsync` reported `false` for file descriptors (such as pipes or sockets) that genuinely had `O_NONBLOCK` set. This caused APIs and user code that relied on this property to make incorrect decisions. Accurate `IsAsync` reporting was also a prerequisite for the new `SafeFileHandle.CreateAnonymousPipe` API to correctly expose per-end async semantics on Unix. For more information, see [dotnet/runtime#125220](https://github.com/dotnet/runtime/pull/125220). | ||
|
|
||
| ## Recommended action | ||
|
|
||
| Review any code that checks `SafeFileHandle.IsAsync` or `FileStream.IsAsync` on Unix and takes action based on the result: | ||
|
|
||
| - If your code assumed `IsAsync` was always `true` on Unix for files opened with `FileOptions.Asynchronous`, update it to account for the fact that regular file handles now return `false`. | ||
|
|
||
| - If you wrap a non-blocking `SafeFileHandle` in a `FileStream` (for example, one created via `SafeFileHandle.CreateAnonymousPipe` with `asyncRead: true` or `asyncWrite: true`), `FileStream.IsAsync` might now return `true` where it previously returned `false`. Adjust downstream code accordingly. | ||
|
|
||
| - If you construct `SendPacketsElement` with a `FileStream` on a non-Windows platform and previously expected an `ArgumentException` to be thrown for non-async streams, note that the exception is no longer thrown. Guard any such expectation with `OperatingSystem.IsWindows()`. | ||
|
|
||
| ## Affected APIs | ||
|
|
||
| - <xref:Microsoft.Win32.SafeHandles.SafeFileHandle.IsAsync?displayProperty=fullName> | ||
| - <xref:System.IO.FileStream.IsAsync?displayProperty=fullName> | ||
| - <xref:System.Net.Sockets.SendPacketsElement.%23ctor(System.IO.FileStream,System.Int64,System.Int32)?displayProperty=fullName> | ||
| - <xref:System.Net.Sockets.SendPacketsElement.%23ctor(System.IO.FileStream,System.Int64,System.Int32,System.Boolean)?displayProperty=fullName> | ||
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
Oops, something went wrong.
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.
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.
The "Reason for change" section says
SafeFileHandle.IsAsyncpreviously reportedfalsefor genuinely non-blocking file descriptors (pipes/sockets withO_NONBLOCK), but the "Previous behavior" section only describes the regular-file scenario. Add a sentence in "Previous behavior" clarifying the pipe/socket case (and that the value was based on the open options rather than actualO_NONBLOCK) so the narrative doesn’t read as contradictory.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 apply changes based on this feedback
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.
Updated in 2a1f4e0. The "Previous behavior" section now explicitly describes both scenarios: regular files returning
truedespite noO_NONBLOCK(values based on open options), and genuinely non-blocking pipes/sockets incorrectly returningfalse.