Skip to content

net: ignore stale lookup callbacks after reconnect - #65239

Open
wehamed wants to merge 1 commit into
nodejs:mainfrom
wehamed:net-fix-stale-lookup-after-reconnect
Open

net: ignore stale lookup callbacks after reconnect#65239
wehamed wants to merge 1 commit into
nodejs:mainfrom
wehamed:net-fix-stale-lookup-after-reconnect

Conversation

@wehamed

@wehamed wehamed commented Aug 12, 2026

Copy link
Copy Markdown

Fixes: #55519

A DNS lookup started before Socket.destroy() can complete after the
same socket has been reconnected. Because reconnecting sets
connecting back to true, the stale callback can pass the existing
guard and initiate another connection using the new lifecycle's handle,
resulting in errors such as EALREADY.

This change tracks the socket connection generation across destroy
boundaries. Lookup and autoSelectFamily callbacks captured by an older
generation are ignored after the socket is reused.

The regression test uses a local server and controlled lookup callbacks
to reproduce the stale-callback ordering deterministically.

Tests:

  • regression test on unmodified source reproduces EALREADY
  • focused net tests
  • autoSelectFamily tests
  • net regression sweep
  • ESLint
  • git diff --check

A DNS lookup started before Socket.destroy() can complete after the
same Socket has been reconnected. Since reconnecting sets `connecting`
back to true, the stale lookup callback can act on the new connection
lifecycle and start another connect on its handle.

Track the socket connection generation across destroy boundaries and
ignore stale lookup and autoSelectFamily callbacks from an earlier
generation.

Signed-off-by: Hamed Elaraby <qef0@hotmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EALREADY when reconnecting socket after destroying immediately after connecting

2 participants