Skip to content

fix(client): prevent late connections after shutdown - #3499

Merged
nkaradzhov merged 4 commits into
redis:masterfrom
jakobzach:jakob/fix-late-connect-after-destroy
Oct 19, 2026
Merged

nkaradzhov merged 4 commits into
redis:masterfrom
jakobzach:jakob/fix-late-connect-after-destroy

Conversation

@jakobzach

@jakobzach jakobzach commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3498.

Destroying a client during automatic reconnect can miss the pending replacement TCP socket, which later connects and revives the closed client. Reject and destroy that late socket before assigning it or emitting connect/ready, using the existing isOpen flag.

The socket regression first awaits a successful connection, triggers automatic reconnect, destroys after reconnecting while the replacement TCP socket is pending, then verifies that the late socket is destroyed without events or a second initiator call.

The four-line public close() cleanup is required alongside the guard: without it, closing before ready with queued commands rejects connect() but leaves the commands and close() pending. Focused public-client tests cover that settlement and preserve graceful draining after readiness. README documents the distinction.

Validation

  • The requested reconnect regression fails on current upstream master f6e267fcacd735992bcf8c2b3be17f48eed6e00b and passes with the guard.
  • 24 focused socket/close tests pass, excluding Docker-backed socketTimeout tests and the existing retry-counter failure.
  • Build, client type tests and ESLint for all changed TypeScript files pass.
  • The existing sequential retry-counter test fails identically on current upstream master (connections[0] is undefined). Docker-backed tests were not rerun in this follow-up.

Run the requested regression with:

npm run test-single -- packages/client/lib/client/socket.spec.ts --grep 'replacement TCP connection is pending'

Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

@nkaradzhov

Copy link
Copy Markdown
Collaborator

Thanks, we will take a look

@nkaradzhov

Copy link
Copy Markdown
Collaborator

Thanks for the careful work and the tests. We've decided not to take this. The full fix needs extra connection-state tracking in the socket, and we don't think it's worth that for this case. It only happens if you destroy or close a client before connect() has finished. Awaiting connect() first avoids it. If you hit this in a real app where awaiting isn't possible, please tell us about it and we'll take another look.

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakobzach could you add one socket-level regression test for the reconnect path? The test should destroy after reconnecting, then let the replacement socket connect, and assert that it is destroyed and that no connect/ready events fire. The current tests cover only the first connect.\n\nAfter that, I'll review it promptly. Thanks again for the careful report.

@jakobzach

Copy link
Copy Markdown
Contributor Author

Added the requested reconnect regression in abf5b62. It awaits initial readiness, destroys after automatic reconnect starts, then verifies the late socket is destroyed with no connect/ready events or second initiator call. It fails on current master and passes with the guard.

24 focused tests, build, client type tests and lint pass. The PR description also explains why the existing pre-ready close() cleanup is necessary to avoid stranding queued commands. Could you take another look?

…ining

Guard the pending socket on a teardown counter bumped by destroySocket()
instead of on #isOpen. destroy() and close() with an empty queue still drop
the late socket, but close() with queued commands lets it connect and drain,
as before. This removes the need to turn close() before ready into destroy().
@nkaradzhov

Copy link
Copy Markdown
Collaborator

Hey @jakobzach, thanks! I pushed a small tweak on top. I should have asked for it earlier, but I didn't, so I fixed it myself instead of sending you on another round.

TLDR: the late-socket guard now fires only after a real teardown, so close() with queued commands still drains as before. That let me drop the close() → destroy() change and the README line. The tests are updated to match.

@nkaradzhov
nkaradzhov marked this pull request as ready for review October 19, 2026 10:31
@nkaradzhov
nkaradzhov merged commit 1795839 into redis:master Oct 19, 2026
14 checks passed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T10:38:16.398771Z 9bd8a57 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9bd8a572cb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

try {
const connectStartTime = performance.now();
const socket = this.#socket = await this.#createSocket();
const teardownEpoch = this.#teardownEpoch;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Carry teardown state across the reconnecting callback

When close() with an empty queue or destroy() is called synchronously from a client's reconnecting listener after a ready connection fails, #onSocketError invokes #connect(1) only after that listener returns. This line therefore records the already-incremented epoch, the comparison below succeeds, and the replacement can emit connect/ready for the closed client; preserve the epoch from before emitting reconnecting or suppress the post-listener attempt when teardown ran.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nkaradzhov should I open a follow-up PR for this? I’d suggest checking whether the callback shut the client down before starting another connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Late TCP connection after destroy or close revives a closed client

2 participants