fix: normalize ENOTSOCK/EBADF to os.ErrClosed on macOS TUN close#11
Open
Eninspace wants to merge 1 commit intoMetaCubeX:metafrom
Open
fix: normalize ENOTSOCK/EBADF to os.ErrClosed on macOS TUN close#11Eninspace wants to merge 1 commit intoMetaCubeX:metafrom
Eninspace wants to merge 1 commit intoMetaCubeX:metafrom
Conversation
On macOS, closing a utun device while batch reader/writer goroutines
are active causes recvmsg_x/sendmsg_x/writev to return ENOTSOCK or
EBADF (depending on timing). These are not recognized by E.IsClosed(),
causing error spam in logs ("batch read packet: socket operation on
non-socket" / "batch read packet: bad file descriptor").
This normalizes both errors to os.ErrClosed at the source, following
the same pattern already used for EBADFD on Linux (tun_linux.go:90,248).
macOS utun is a socket (PF_SYSTEM), unlike Linux TUN which is a file
(/dev/net/tun), so the errno values differ:
- Linux: EBADFD (file descriptor in bad state)
- macOS: ENOTSOCK (socket op on non-socket) + EBADF (bad fd)
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.
Problem
On macOS, closing a utun device while batch reader/writer goroutines are active causes
recvmsg_x/sendmsg_x/writevto returnENOTSOCKorEBADF(depending on timing). These are not recognized byE.IsClosed(), causing error spam in logs:Root cause
macOS utun is a socket (
socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)), unlike Linux TUN which is a file (/dev/net/tun). When the utun fd is closed, the errno values differ:EBADFD(already handled intun_linux.go:90,248)ENOTSOCK+EBADF(not handled)Fix
Normalize
ENOTSOCKandEBADFtoos.ErrClosedintun_darwin.go, same pattern asEBADFDhandling intun_linux.go.3 locations (all places that return raw errno from utun syscalls):
BatchRead()—BlockingRecvMMsgUntilStoppedBatchWrite()non-MsgX path —NonBlockingWriteIovecBatchWrite()MsgX path —NonBlockingSendMMsgChanges
tun_darwin.go"errors"import