Skip to content

fix(cluster): drop the source connection when the in-flight flush runs during slot migration - #3642

Merged
nkaradzhov merged 1 commit into
redis:masterfrom
Cdineshreddy12:fix/smigrated-desynced-socket
Nov 19, 2026
Merged

nkaradzhov merged 1 commit into
redis:masterfrom
Cdineshreddy12:fix/smigrated-desynced-socket

Conversation

@Cdineshreddy12

@Cdineshreddy12 Cdineshreddy12 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3635

Implements the close-and-reconnect direction from my comment on the issue — if maintainers prefer a different approach happy to rework.

Two pieces:

  • waitForInflightCommandsToComplete now resolves a boolean: true only when the timeout fired and pending commands were actually flushed. The one other caller (enterprise-maintenance-manager) ignores the return value, so nothing else changes.
  • In the SMIGRATED path, every client whose flush ran gets its connection dropped before the unpause, via a new internal _resetDesyncedConnection that resets the decoder and calls socket.failConnection() — the same error-and-reconnect treatment a decoder error already gets in #attachListeners, since it's the same situation: replies on the wire that can no longer be matched to commands. The regular reconnect machinery brings up a clean connection and handles the pub/sub clients' resubscribes.

A source that lost all its slots is untouched by this — its clients get destroy()ed in step 7 anyway, so late replies die with them. Same for the shift() on an empty queue turning into a decoder TypeError: nothing old can arrive on the new socket.

Tests: four unit tests in commands-queue.spec.ts covering the new return value (nothing in flight, completed within timeout, flushed on timeout, timeout without flushOnTimeout). The cluster-side behavior needs a migrating cluster to exercise end-to-end, which the docker harness can't do on my machine (--network host on Docker for Mac, same limitation I hit on #3641) — the full queue spec (74 tests) and the build pass locally, and I verified the flush path rejects with TimeoutError as before.


Note

Medium Risk
Changes cluster slot-migration connection teardown when in-flight commands are flushed; incorrect handling could mis-pair RESP replies or leave a bad connection alive, but scope is limited to the SMIGRATED timeout path and sources that still retain slots.

Overview
Fixes #3635 by treating a timeout-driven in-flight command flush during cluster slot migration as a desynced socket that must not be reused.

waitForInflightCommandsToComplete now returns a boolean (true only when the wait timed out and flushOnTimeout actually flushed pending replies). Cluster SMIGRATED handling uses that signal: any source/pub-sub client that was flushed gets _resetDesyncedConnection (decoder reset + failConnection, same path as a decoder error) before the source is unpaused, so late server replies cannot pair with the wrong commands on positional RESP.

Adds four queue unit tests for the new return semantics.

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

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T11:58:02.374058Z 6245561 New commits
ℹ️ 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.

@nkaradzhov

Copy link
Copy Markdown
Collaborator

Thanks. #3641 is merged, so this PR now conflicts with master. Please rebase on master and drop the first commit (e4598a4c), so only the slot-migration fix is left.

…s during slot migration

When the 5s in-flight wait times out during SMIGRATED handling, the
pending commands are rejected and the reply queue is emptied, but the
socket stays open. The server may still send the replies for the
flushed commands, and since RESP pairing is positional the queue hands
them to whatever was sent after the flush - late replies resolve the
wrong commands, or crash the decoder on shift() when nothing was sent.

waitForInflightCommandsToComplete now reports whether it flushed, and
the SMIGRATED path drops the connection of every client whose flush ran
before unpausing it, through the same error-and-reconnect handling a
decoder error uses. A fresh connection gets no late replies. A source
that lost all its slots is unaffected - its clients are destroyed
anyway.

Fixes redis#3635
@Cdineshreddy12
Cdineshreddy12 force-pushed the fix/smigrated-desynced-socket branch from 8d0cef2 to 6245561 Compare November 19, 2026 11:51
@Cdineshreddy12

Copy link
Copy Markdown
Contributor Author

Done — rebased on master and dropped the #3641 commit, so this is now just the slot-migration fix (commit 6245561). Build and the queue unit tests are green. Thanks for merging #3641!

@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.

Thanks, LGTM.

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.

Cluster slot migration: late replies go to the wrong commands after the in-flight flush

2 participants