Fix macOS crash when accept() returns zero-length address#2389
Merged
Conversation
danlapid
commented
Aug 17, 2025
Member
|
Let's use #2365, it's a more polished fix and includes a test. It just needs a couple comments addressed on the test. |
a441886 to
395da92
Compare
Collaborator
Author
|
Pushed commit to use the exact code of https://github.com/capnproto/capnproto/pull/2365/files , no code changes as of yet so review comments are not yet addressed. |
395da92 to
87ba999
Compare
kentonv
reviewed
Aug 18, 2025
On macOS, accept() can return a valid file descriptor with addrlen=0 when a connection is aborted during the accept (e.g., during "happy eyeballs" dual-stack connection attempts). This is a bug in XNU (the macOS kernel). This commit: 1. Adds special handling for zero-length address returns on macOS 2. Adds platform-specific checks for socket connection errors 3. Implements more robust error detection in socket accept logic 4. Includes comprehensive tests for aborted socket connections The changes improve socket connection handling, particularly on macOS, by gracefully managing scenarios where connections are aborted before being fully accepted. Based on PR #2365 by Aaron O'Mullan
87ba999 to
85c8f90
Compare
kentonv
approved these changes
Aug 20, 2025
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.
As reported in cloudflare/workerd#4623
On macOS, accept() can return a valid file descriptor with addrlen == 0 when a connection is aborted during the accept (e.g., during "happy eyeballs" dual-stack connection attempts). This was causing workerd to crash with:
Fatal uncaught kj::Exception: kj/async-io.c++:3126:
failed: expected addrlen >= sizeof(addr->sa_family) [0 >= 1]
This fix:
Fixes cloudflare/workerd#4623
🤖 Generated with Claude Code
Tried to produce a test for this but was unable to as it seems to be a race condition.
At least I was able to reproduce the issue locally and prove that this change fixes it.