Repository navigation
fix(cluster): stay ended when nodes removed by disconnect() end late - #2237
Merged
PavelPashov merged 1 commit intoOct 20, 2026
Merged
Conversation
disconnect() empties the connection pool before the node connections have closed. Each node's later "end" then finds the pool empty and emits "drain", and every drain set the cluster status to "close". The first one closes and ends the cluster, the rest move the ended cluster back to "close", where sendCommand() queues commands instead of rejecting them, so they never settle. Ignore a drain once the cluster has ended. A drain in any other status still closes the cluster, so quit(), reconnects and the aborted-connect path from redis#2217 are unchanged.
Codex Review SummaryThis 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. |
PavelPashov
approved these changes
Oct 20, 2026
PavelPashov
left a comment
Contributor
There was a problem hiding this comment.
@maxymlyskov LGTM thank you for the contribution
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.
After
cluster.disconnect(),cluster.statusends atcloseand every later command waits in the offline queue and never settles. The README saysend"emits afterclosewhen no more reconnections will be made", but on mainclosefires again afterend.Cause:
disconnect()callsconnectionPool.reset([]), which removes every node in the same loop that disconnects them. Each node'sendlands later, finds the pool empty and emitsdrain, and every drain setsclose. The first goes throughhandleCloseEventtoend, the rest setcloseagain.sendCommandonly rejects inend, so inclosethe command gets queued.quit()lets each node remove itself onend, so it drains once and staysend.Fix: the
drainhandler returns early whenthis.status === "end", the same guardconnect()andsendCommand()use. 3 insertions inlib/cluster/index.ts. Other statuses still getclose, so #2217'shandleAbortedConnectpath is unchanged.connect()afterdisconnect()still works (checked after the nodes end and in the same tick). One difference: it now starts fromend, so it resetsretryAttemptslike afterquit().Left as is: several drains before the close listener runs still emit duplicate
closeevents beforeend, though the status ends atend. A pool that drains once per emptying would fix it too, but that's bigger (#1864 tried it in the pool, closed unmerged), so I kept the guard in the cluster.Real cluster (3 masters + 3 replicas), same in 3 of 3 runs:
Tests:
test/functional/cluster/disconnection.ts, "should stay ended when the nodes end after disconnect": afterdisconnect()it waits for every node'sendand asetImmediate, then expectsendand a rejectedget. Waiting on the cluster'sendalone passes on main, the nodes end after it. On main:npm run test:js(Redis 8.10.2): 2883 passing, 25 pending, 0 failing. main: 2881 passing, 1 failing (autoPipelining for single node "should handle large pipelines without RangeError", 8000 ms timeout; passed on the branch run).npm run test:cluster: 66 passing on both.npx tsd,prettier --checkclean. lint: 0 errors, 23 warnings, same as main.Note
Low Risk
Small guard on an existing lifecycle path; behavior aligns disconnect with quit and only affects post-disconnect status and command rejection.
Overview
Fixes a bug where
cluster.disconnect()could leave the client incloseinstead ofendwhen node connections finish shutting down after the pool is already empty.The connection pool
drainhandler now skipssetStatus("close")ifstatusis alreadyend, matching guards used inconnect()andsendCommand(). That stops late drains (from each node’sendafterconnectionPool.reset([])) from regressing the cluster toclose, where commands sit in the offline queue instead of rejecting with "Connection is closed."Adds a functional test that **
disconnect()**s a multi-node mock cluster, waits for every nodeend, and assertsstatus === "end"andget()rejects.Reviewed by Cursor Bugbot for commit e41b22c. Bugbot is set up for automated code reviews on this repo. Configure here.