Repository navigation
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 959adc9464
ℹ️ 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".
|
Nice — this is the connected-then-gave-up half of #3396. Heads-up on an overlap: my #3652 rewrites More usefully: my current rewrite would actually regress your case. It returns Happy to coordinate however you'd prefer — rebase mine onto yours once it lands, or pull your guard into #3652. Either way your fix should stand. |
|
Thanks, good catch on the overlap. Happy with whichever order the maintainers prefer. If #3652 lands first I'll rebase #3656 onto it; in your synchronous |
|
Perfect — I'll fold the guard in right there when I rework #3652, and if yours lands first the rebase plus your spec will keep me honest. Thanks, teddy. |
|
Thanks for the An update on the overlap, since it resolved in your favour: nkaradzhov picked the bounded-wait option on #3592, so #3652 no longer rewrites I checked the branches and they merge cleanly in either order — your guard sits at the top of |
|
Closing this, as it does not follow our contributing guidelines (https://github.com/redis/node-redis/blob/master/CONTRIBUTING.md#before-starting-work). We doubt this comes up in a real application: the default reconnect strategy gives up only on a socket timeout, and in practice |
|
Thanks for taking a look. I've opened #3674 with our production configuration and the impact, as you asked. In short, our |
|
Glad this is reopened — the production numbers in #3674 make the case well. Since #3652 touches this file, here is where the two stand now, so neither of you has to work it out from the diff.
The node client's handler chain in Two consequences:
No ordering constraint from my side either way. |
A node client that connected and later gave up reconnecting (e.g. after a socket timeout) stayed in node.client, so nodeClient() kept returning it and every command for the node's slots failed with ClientClosedError until the cluster client was rebuilt. Drop and destroy the client on its 'terminated' event, which fires only when reconnectStrategy gives up, never on close() or QUIT, so a draining client is left alone. The next nodeClient() call connects a fresh one. Fixes redis#3674
db41dd5 to
8605413
Compare
|
Thanks for reopening it. I've reworked it as you suggested:
I rebased onto master and squashed it into one commit. @Cdineshreddy12 thanks for the heads-up about #3652. The listener sits next to the |
|
Read through The first-connect spec is the one I would have asked for, since that is the case where both the listener and the catch in One thing worth a comment rather than a change, because it reads as covered when it is not. I am not suggesting you take that on in this PR. Nikolay and I have an open thread on #3652 about the pub/sub node pinning to a node it cannot reach, and this is the terminal variant of it, so it probably belongs with that decision. A line in the comment noting the listener is node-client-only would stop the next reader assuming pub/sub is handled. Agreed on the conflict: your listener sits right next to the |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 860541366d
ℹ️ 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".
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 8605413. Configure here.
…n a node gives up - Destroy the terminated client in a microtask, after the socket error has rejected its pending commands with the real cause, not DisconnectsClientError. - #handleSmigrated keeps the source client for the whole migration, so a source that terminates while in-flight replies are awaited no longer throws and leaves the emptied source in masters/nodeByAddress. - nodeClient() no longer opens a connection after close()/destroy(), for a caller still holding a node whose client was dropped. - Note that the 'terminated' handler covers node data clients only.
|
@Cdineshreddy12 thanks for reading it closely. Good point about the pub/sub clients: the handler returns straight away for them, because e4c68c7 also addresses Codex's three findings on 8605413, each with a spec that fails on 8605413:
|
|
Thanks for adding the node-client-only line — that was the only thing I wanted from it. I actually merged the two branches locally to check the conflict we both predicted, and we were both wrong about where it is.
Both branches also merge cleanly against current master on their own, so there is still no ordering constraint — just that one line to join up afterwards. One note on the rework, since I had cited it: The |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e4c68c7bfb
ℹ️ 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".
| // Only a node's data client is handled here: for the pub/sub clients this | ||
| // method also builds, node.client is the data client, so this returns. | ||
| if (node.client !== client) return; | ||
| node.client = undefined; |
There was a problem hiding this comment.
Process migrations after the source data client terminates
If the data client terminates before an SMIGRATED notification arrives on another live node or Pub/Sub connection, this assignment makes #handleSmigrated() skip the entire source entry through its sourceNode.client === undefined guard. The handler has already recorded the sequence ID, so later copies are ignored too: moved slots remain routed to the source, which also remains selectable for keyless and fan-out commands. A two-node comparison processes the same notification on the parent but leaves the source unchanged here. Run the topology updates and source cleanup without a data client, skipping only the queue-transfer work.
Useful? React with 👍 / 👎.
| this.#reconnectionTracker.removeClient(sourceNode.client?._clientId); | ||
| sourceNode.client.destroy(); | ||
| this.#reconnectionTracker.removeClient(sourceClient._clientId); | ||
| sourceClient.destroy(); |
There was a problem hiding this comment.
Dispose replacement clients when removing a migration source
If the source terminates while a full-slot migration is awaiting in-flight replies, a concurrent nodeClient(sourceNode) lookup can install a replacement before cleanup resumes. Cleanup removes the node from masters and nodeByAddress, but this line destroys only the captured original sourceClient, leaving the replacement in sourceNode.client open and untracked. In a two-node runtime check, that connection remains open even after slots.destroy(). Retain the stable reference for queue operations, but also prevent or dispose replacement clients when removing the source.
Useful? React with 👍 / 👎.
nkaradzhov
left a comment
There was a problem hiding this comment.
Thanks. The terminated approach looks right. Two small changes, please:
- Leaked client after a full-slot migration. If the source terminates while
#handleSmigratedwaits for in-flight replies, a concurrentnodeClient(sourceNode)can install a new client. Cleanup then destroys onlysourceClient, so the new one stays open and untracked, even afterdestroy(). At cleanup, please also remove and destroysourceNode.clientwhen it is set and is notsourceClient. - Migration spec.
finishes a slot migration whose source client terminates mid-migrationclearssourceNode.clientby hand. Please make the source client emit a realterminatedinstead, so the spec exercises the listener.
We're fine with Codex's other finding on e4c68c7 (SMIGRATED skipped when the source has no client): the next command to that node gets MOVED, which refreshes the topology. The hang on a node that stays down is #3592, which #3652 covers.

Description
Follow-up to #3403, which fixed the first-connect half of #3396. This covers the other half: a cluster node client that connected successfully and later gave up.
When a connected node client's
reconnectStrategystops (returnsfalse/Error), for example on a socket timeout, which the default strategy declines to retry, the client is closed for good and emitsterminated.nodeClient()only creates a new client whennode.clientis falsy, so it keeps returning the closed one, and every command for that node's slots fails withClientClosedError: The client is closed. The #3403 fix doesn't reach this path, because the originalconnect()resolved long ago. The only ways out are a topology change that drops the node's address, or recreating the whole cluster client. Users have reported this since #2141 ("ClientClosedError forever until the application is restarted", closed as stale), and again in a comment on #2948.We hit it in production with
socketTimeoutset: a shard stalls for about a minute, the idle node connection times out, and that shard's slots then fail for minutes after the shard has recovered, until the application rebuilds the cluster client.Change
The node client is dropped and destroyed when it emits
terminated, as suggested in #3674. That event fires only whenreconnectStrategygives up, never onclose()orQUIT, so a client still draining is left alone without extra checks. The nextnodeClient()call then connects a fresh client through the existing path. In#createClient:The identity check keeps a newer client that
#discovermay have swapped in. The client is destroyed in a microtask, after the socket error has rejected its pending commands with the real cause. The handler covers node data clients only; the pub/sub clients#createClientalso builds are left as they are (see #3652).Two supporting changes, from review:
#handleSmigratedkeeps the source client in a local for the whole migration, so a source that terminates mid-migration doesn't break the cleanup.nodeClient()doesn't open a connection afterclose()/destroy()(a flag those set andconnect()clears), for a caller still holding a node whose client was dropped.Notes
reconnectStrategythat can give up, which includes the default strategy wheneversocketTimeoutis set. Default-configured clients never reach this path.connectPromise, and each attempt is bounded byconnectTimeout/socketTimeout, which matches what fix(client): recreate cluster node client after a terminal connect failure #3403 does for first-connect failures. Happy to add a per-node backoff if you'd prefer one.cluster-slots.spec.ts, no Redis server; a fake RESP2 node like the existing discovery tests):replaces a node client whose reconnectStrategy gave up after it connected: connects a real cluster to the fake node, drops the connection withreconnectStrategy: false, then assertsnodeClient()returns a new, open client. It fails without the fix (the closed client must be replaced).keeps a node client that is still draining after close(): a PING is held by the server, the client is closed, andnodeClient()must still return it and the PING must resolve.does not reconnect a closed node client after the cluster is destroyed:destroy()doesn't emitterminated, so no new connection is opened.rejects pending commands with the socket failure when the node gives up: a held PING rejects withSocketClosedUnexpectedlyError, notDisconnectsClientError.does not open a node connection after the cluster is destroyed, once the node gave up.finishes a slot migration whose source client terminates mid-migration.drops a node client that gives up during its first connect, once, and retries on the next call: the path that runs both the listener and the catch in#createNodeClient. It asserts the real connect error surfaces (notClientClosedError),terminatedfires once,node.clientis cleared, and the next call creates a fresh client.Checklist
npm testpass with this change (including linting)?cluster-slots.spec.tspasses (24), and eslint andtscare clean on the changed files. I haven't run the full Docker-backed suite.Note
Medium Risk
Changes core cluster connection lifecycle and SMIGRATED handling; behavior is heavily tested but affects all shard routing after terminal disconnects.
Overview
Fixes cluster shards getting stuck on
ClientClosedErrorafter a node connection dies for good (e.g.socketTimeoutwith defaultreconnectStrategy).nodeClient()used to keep returning the same closed client until topology refresh or a full cluster rebuild.A
terminatedlistener on each node data client now clearsnode.client, removes the client from the reconnection tracker, and **destroy()**s it on the next microtask so in-flight commands see the real socket error, notDisconnectsClientError. Clients still draining afterclose()``** are untouched because **terminated` does not fire there.#shutDownis set ondestroy()/ graceful teardown and cleared onconnect(), so latenodeClient()calls reject withClientClosedErrorinstead of opening untracked connections.Slot migration (
#handleSmigrated) pinssourceClientfor the whole handler so a mid-migration terminate does not leave **TypeError**s or stale nodes inmasters/nodeByAddress.New unit tests cover replacement after give-up, draining clients, shutdown guards, first-connect termination, error surfacing, and migration when the source terminates mid-flight.
Reviewed by Cursor Bugbot for commit e4c68c7. Bugbot is set up for automated code reviews on this repo. Configure here.