[NODERAWSOCKETS] Bind-first connect for synchronous getsockname - #27566
Merged
sbc100 merged 1 commit intoAug 27, 2026
Merged
Conversation
sbc100
approved these changes
Aug 19, 2026
An unbound TCP client connect created a plain async net.Socket, with saddr/sport only recorded on the async 'connect' event, so getsockname() immediately after a non-blocking connect() returned 0.0.0.0:0. Kernel semantics assign the ephemeral source port synchronously at connect(), so callers (e.g. mio) that read the local address right after a non-blocking connect raced the event loop. connect() now binds an ephemeral port first through the same eager synchronous bindHandle path an explicit bind() takes, then connects through the bound handle, making getsockname() correct up front.
guybedford
force-pushed
the
noderawsockets-connect-getsockname
branch
from
August 19, 2026 15:53
468d477 to
f9fedd7
Compare
Collaborator
Author
|
@sbc100 this one still has approval, is it ok for me to land? |
sbc100
reviewed
Aug 27, 2026
| server.server_close() | ||
| thread.join() | ||
|
|
||
| def test_noderawsockets_connect_getsockname(self): |
Collaborator
There was a problem hiding this comment.
Would it make sense to rename all the tests in this file from test_noderawsockets_xxx to just test_xxx not that they are in their own file? (Or are not all of them raw socket tests)?
Collaborator
Author
There was a problem hiding this comment.
Yeah good point, I can include this in a subsequent cleanup.
sbc100
approved these changes
Aug 27, 2026
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.
This fixes an ordering issue in the NODERAWSOCKETS TCP client connect path.
An unbound client connect() created a plain async
net.Socket, withsaddr/sportonly recorded on the async'connect'event, sogetsockname()immediately after a non-blockingconnect()returned0.0.0.0:0. Kernel semantics assign the ephemeral source port synchronously atconnect(), so callers that read the local address right after a non-blocking connect raced the event loop (reliably failing under load).connect()on an unbound socket now binds an ephemeral port first, through the same eager synchronousbindHandlepath an explicitbind()takes, then connects through the bound handle:getsockname()reports the assigned port synchronously afterconnect(), matching kernel behaviornet.BoundSocket, or thetcp_wrapfallback on older node) are synchronous, so this works across node versionsTest coverage:
test_noderawsockets_connect_getsocknameasserts the ephemeral port is non-zero immediately after a non-blockingconnect()returns, before any event loop turn, and that it is unchanged once connected. The test is plain POSIX and passes natively against the host stack.Made with AI assistance under my review