Skip to content

fix(cluster): end when quit() or disconnect() is called while reconnecting - #2240

Merged
PavelPashov merged 2 commits into
redis:mainfrom
maxymlyskov:fix/end-cluster-when-quit-while-reconnecting
Oct 21, 2026
Merged

PavelPashov merged 2 commits into
redis:mainfrom
maxymlyskov:fix/end-cluster-when-quit-while-reconnecting

Conversation

@maxymlyskov

@maxymlyskov maxymlyskov commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

When every node connection drops, a Cluster goes to reconnecting with an empty pool. If you call quit() or disconnect() at that point, it stays in disconnecting for good: end never fires and anything sent afterwards sits in the offline queue.

Cause: the pool's drain handler (L255-L260) sets close, the one-shot close listeners from connect() (L393-L394) fire, and handleCloseEvent() (L1099) sets reconnecting and arms the retry timer. The pool is empty by then.

disconnect() (L425-L451) and quit() (L456-L500) set disconnecting and clear the timer, but they only move on by hand from wait. Every other status waits for the pool's drain. And reset([]) on an empty pool emits nothing, with no close listener left, so nothing ever ends the client. sendCommand queues while disconnecting, so later commands just hang.

Repro on a real Redis 8.6 cluster (3 masters + 3 replicas): kill this client's connections on all 6 nodes with CLIENT KILL, then call quit() or disconnect() on the reconnecting event, with clusterRetryStrategy: () => 1500. On main all 6 runs (3 of each) hung:

# built main 46b3191, Redis 8.6 cluster 3000-3005 (ioredis-cluster, fresh)
## quit
[  192ms] quit() resolved OK status = disconnecting
[  193ms] killed this client's connections on all nodes
[ 2695ms] after 2.5s: status = disconnecting sawEnd = false
[ 4699ms] get() after quit: PENDING after 2000ms
[ 4700ms] ACTUAL: status = disconnecting , end emitted = false , get -> PENDING after 2000ms
RESULT: BUG REPRODUCED
## disconnect
[  169ms] event: disconnecting status= disconnecting
[  170ms] killed this client's connections on all nodes
[ 2672ms] after 2.5s: status = disconnecting sawEnd = false
[ 4675ms] get() after disconnect: PENDING after 2000ms
[ 4675ms] ACTUAL: status = disconnecting , end emitted = false , get -> PENDING after 2000ms
RESULT: BUG REPRODUCED

Fix: both methods take the existing wait branch for reconnecting too, so they run setStatus("close") + handleCloseEvent(). With manuallyClosing set, that sets end and flushes the offline queue with "None of startup nodes is available". quit() keeps its setImmediate and still resolves "OK". 3 insertions, 2 deletions in lib/cluster/index.ts.

I left disconnect(true) alone. Its timer isn't cleared and still calls connect(), so running handleCloseEvent() on top would call clusterRetryStrategy a second time and arm a second timer.

Same cluster with the fix, 6 of 6 end:

## quit
[ 2722ms] get() after quit: rejected: Connection is closed.
[ 2723ms] ACTUAL: status = end , end emitted = true , get -> rejected: Connection is closed.
RESULT: NOT REPRODUCED
## disconnect
[ 2685ms] get() after disconnect: rejected: Connection is closed.
[ 2686ms] ACTUAL: status = end , end emitted = true , get -> rejected: Connection is closed.
RESULT: NOT REPRODUCED

handleAbortedConnect() from #2217 (L1081-L1094) handles the same "disconnecting with an empty pool" case, only inside a connect() aborted while resolving the startup nodes. This uses the same close + handleCloseEvent() pair.

Behaviour change: a cluster you quit() or disconnect() while it's reconnecting now emits end, and its queued commands get rejected. I can't think of code that depends on the hang, but it is a visible change.

Tests:

  • 3 new tests in test/functional/cluster/disconnection.ts, with MockServer, next to the existing one for nodes ending after disconnect(). With lib/cluster/index.ts at main, the disconnect() and quit() ones fail:

      4 passing (656ms)
      2 failing
    
      1) disconnection
           should end when disconnect() is called while reconnecting:
    
          AssertionError: expected 'disconnecting' to equal 'end'
    
      2) disconnection
           should end when quit() is called while reconnecting:
    
          AssertionError: expected 'disconnecting' to equal 'end'
    
  • The third, "should not retry again when disconnect(true) is called while reconnecting", passes on main and on the branch. It pins that disconnect(true) still waits for its pending reconnect (clusterRetryStrategy called once).

  • npm run test:js (Redis 8.10.2, Node 22): 2908 passing, 25 pending, 1 failing. The failure is autoPipelining for single node "should handle large pipelines without RangeError", an 8000 ms timeout that also fails on main now and then and passes alone.

  • npm run test:cluster against a real 6-node cluster: 71 passing.

  • npm run build passes. npm run lint: 0 errors, the 23 warnings already on main. prettier --check clean on both files.


Note

Medium Risk
Changes cluster teardown semantics during reconnecting; callers that relied on staying in disconnecting will now get end and rejected commands, but this fixes a stuck-state bug in connection lifecycle handling.

Overview
Fixes a hang when quit() or disconnect() is called while the cluster is in reconnecting with an empty connection pool (no node left to emit pool drain). Those paths now mirror the existing wait handling: they drive close and handleCloseEvent() directly instead of connectionPool.reset([]), so end fires, manuallyClosing prevents another retry, and queued commands are rejected.

disconnect(true) is unchanged—it still does not take this shortcut, so a single pending reconnect from clusterRetryStrategy is not double-scheduled.

Adds functional tests for disconnect/quit during reconnecting and asserts disconnect(true) does not invoke the retry strategy again.

Reviewed by Cursor Bugbot for commit 1866bf1. Bugbot is set up for automated code reviews on this repo. Configure here.

…cting

When every node connection is lost, the cluster goes to "reconnecting" with
an empty connection pool. quit() and disconnect() there set "disconnecting"
and wait for the pool's "drain", which an empty pool never emits, so the
cluster never reaches "end" and later commands stay queued. They now take
the branch "wait" already uses. On a cluster that is already "end", both
keep the status instead of moving it to "disconnecting" for good.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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 ⚠️ Failed 2026-10-21T02:12:29.206136Z
We couldn’t complete this review. Comment @codex review to try again.
fad58d0 PR opened
ℹ️ 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.

Keep the after-"end" status change out of this PR, and trim the tests to
one per code path: disconnect() and quit() while reconnecting, and
disconnect(true) still waiting for its pending reconnect.

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thank you for your contribution

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.

2 participants