-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: os: add O_NONBLOCK #73676
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
Comments
Would it be possible to achieve the same goal by implementing the existing You might want to implement this proposal nonetheless, but SetNonblock may be a way to make immediate progress. |
Unfortunately that function can't be implemented on Windows. One can only set the blocking mode when creating the file descriptor (aka handle). There is no reliable way to change it later on. |
@neild this new flag would also affect the |
What would the user-visible effect of setting O_NONBLOCK be? Are there subtly different behaviors on Unix and Windows systems? How would we document the flag? |
On Windows, the main effect is that I/O will not block and will be cancelable, either for named pipes and normal files. On Linux, that will make
I can think on several differences:
I don't think the documentation of the flag should go into that much detail, as this features are well documented online. Here is my proposal ( package os
const O_NONBLOCK = syscall.O_NONBLOCK // open for nonblocking I/O in Unix and for overlapped I/O on Windows. |
Is it currently true that including If so, would you expect similar situations on Windows to emulate that Unix-like behavior -- returning exactly the same error code -- or would someone writing portable code need to write defensive error handling to properly handle this situation? Is it justified to add a higher-level error value (similar to |
I'm afraid I still don't really understand when you want to set Are there programs that run on Unix and Windows that will want to set A documentation concern is that users may reasonably expect that setting |
In effect, yes: |
My main concerns are:
I'm not necessarily opposed to
|
@apparentlymart Yes, but the
Your gut feeling @neild may be right: we don't understand how
I get your point now. Setting On the other hand, a Unix developer might want to set
I don't see how that could happen. |
Proposal Details
Passing
syscall.O_NONBLOCK
(only defined on Unix OSes) toos.Open
opens the file descriptor in nonblocking mode. This flag is mostly useful when working with named pipes, as they are normally used for asynchronous inter-process communication. There are 680 hits ofsyscall.O_NONBLOCK
on GitHub (prompt).It is currently not possible to use the
os
package to open a named pipe for non-blocking I/O (aka overlapped I/O) on Windows, as thesyscall
package doesn't defineO_NONBLOCK
on that port.The
os
package will start supporting overlapped I/O starting in Go 1.25 (see CL 661795 and #19098), yet there will be no way to open a file for overlapped I/O without using thex/sys/windows
(orsyscall
) packages. Note that, on Windows, it is not possible to change a file descriptor to be non-blocking once it has been created, that property must be specified up-front.I propose to promote
syscall.O_NONBLOCK
to theos
package and map it toFILE_FLAG_OVERLAPPED
on Windows. We could also just definesyscall.O_NONBLOCK
for Windows, but thesyscall
API is frozen and IMO that flag makes sense to be in theos
package, as it will now be portable across most of the supported OSes (except on plan9 and wasi?).@golang/windows @golang/proposal-review
The text was updated successfully, but these errors were encountered: