Use MSG_DONTWAIT instead of fcntl with O_NONBLOCK for sockets in sending/receiving#1727
Open
Lkzeu wants to merge 1 commit intochriskohlhoff:masterfrom
Open
Use MSG_DONTWAIT instead of fcntl with O_NONBLOCK for sockets in sending/receiving#1727Lkzeu wants to merge 1 commit intochriskohlhoff:masterfrom
Lkzeu wants to merge 1 commit intochriskohlhoff:masterfrom
Conversation
…ing/receiving On Linux, FreeBSD, OpenBSD and other systems, it's possible to have a non-blocking behavior on sockets using the flag MSG_DONTWAIT on sending and receiving functions per syscall, without needing to set the socket in a non-blocking state globally using ioctl or fcntl. This change will save a few syscalls to achieve the same results.
vinipsmaker
approved these changes
Mar 13, 2026
Contributor
vinipsmaker
left a comment
There was a problem hiding this comment.
I reviewed the code and tested on FreeBSD with Capsicum enabled (so O_NONBLOCK wouldn't even work if it was being used when it shouldn't).
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.
On Linux, FreeBSD, OpenBSD and other systems, it's possible to have a non-blocking behavior on sockets using the flag
MSG_DONTWAITon sending and receiving functions per syscall, without needing to set the socket in a non-blocking state globally usingioctlorfcntl. This change will save a few syscalls to achieve the same results.Another issue with O_NONBLOCK appears when sandboxing. The O_NONBLOCK state is shared among all copies of a file descriptor¹ and therefore if a sandboxed process tries to disable the non-blocking state of a received/inherited socket in a loop, it could potentially freeze the thread of the parent process.
This change was tested and works on Linux and FreeBSD
¹ https://blog.emilua.org/2025/01/12/software-sandboxing-basics/#_non_blocking_io_on_unix_it_sucks
A minimal echo protocol program
By running strace on the echo program before and after this patch you can see that the two calls to fcntl (to get flags and to set the O_NONBLOCK) for each socket is not called anymore on the patched version.