Skip to content

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

Closed

Description

@nkaradzhov

During a slot migration (SMIGRATED), the cluster client waits up to 5 s for the source node to answer the commands it already sent (cluster-slots.ts, step 6). On timeout it calls waitForInflightCommandsToComplete({ timeoutMs: 5000, flushOnTimeout: true }). That rejects every command in #waitingForReply and then empties the list with reset().

If the source node still owns other slots, the client unpauses it and keeps the same socket. The server still sends the replies for the flushed commands. The queue gives each reply to the command at the head of #waitingForReply, so the late replies go to commands sent after the flush.

Sequence:

SMIGRATED for source S
  S.queue.waitForInflightCommandsToComplete({ timeoutMs: 5000, flushOnTimeout: true })
    after 5 s: GET a, GET b still pending
    reject a and b, then reset() the read queue to empty
  S still owns slots -> S._unpause()   (socket stays open)
  client writes GET c       -> read queue = [GET c]
  late reply to GET a       -> resolves GET c   (wrong value)

If nothing was written after the flush, #waitingForReply.shift()! returns undefined and the decoder throws a TypeError.

The flush also runs on the source node's pub/sub clients.

Possible fixes:

  • Close and reconnect the source socket when the flush runs. A fresh connection gets no late replies.
  • Reject the commands in place and keep their entries, so each late reply is read and dropped. Pub/sub commands need care, because their resolve changes pub/sub state.

This behavior dates from #3142.

Activity

  1. Cdineshreddy12 commented on Nov 19, 2026

    @Cdineshreddy12
    Contributor

    I'd argue for the first option (close and reconnect the source socket when the flush actually runs) and against keeping rejected entries as drop-tombstones, for one main reason: RESP has no request/response correlation, so pairing is purely positional. The tombstone approach only restores pairing if the server eventually sends a reply for every flushed command — but the 5s timeout fired precisely because replies weren't coming. If even one of them never arrives (node hung mid-failover, command swallowed), its tombstone sits at the head of #waitingForReply forever, and every later reply resolves the wrong entry again — same corruption, shifted the other way, plus the pub/sub resolve side effects you mentioned. Once the queue and the wire can disagree, no bookkeeping on the client side can prove they're aligned again; a fresh connection is the only state where pairing is known-good.

    The cost is small because this is already the drastic path — the source node sat on replies for 5 full seconds, so a reconnect on top of that changes little, and the existing reconnect machinery already handles resubscribes for the pub/sub clients.

    Concretely: have waitForInflightCommandsToComplete report whether it flushed, and in the SMIGRATED path reset the source connection instead of plain _unpause() when it did (same for the pub/sub clients the flush ran on). The shift() on an empty queue turning into a decoder TypeError goes away with it, since nothing old can arrive on the new socket.

    Happy to send a PR along these lines if that direction sounds right — a unit test in commands-queue.spec.ts can simulate the late-reply misrouting without a cluster.

  2. added a commit that references this issue on Nov 19, 2026
    6245561
  3. added a commit that references this issue on Nov 19, 2026
    4217b5e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions