Skip to content

Fix cluster abort when a slot map refresh retires the connection handling it (#578) - #584

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/cluster-self-retire-abort
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/cluster-self-retire-abort

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In cluster mode, moving every slot off a live primary mid-run makes memtier retire that primary's connection. With --retry-on-error the run then aborts:

shard_connection.cpp: request* shard_connection::pop_req(): Assertion `m_pending_resp.load(std::memory_order_relaxed) >= 0' failed.

Without --retry-on-error, the same move often ends the run with client(s) stopped before reaching their stop condition.

Root cause

  1. The primary's connection gets -MOVED, so handle_moved() makes that same connection send CLUSTER SLOTS.
  2. It handles the reply inside its own process_response() loop. handle_cluster_slots() sees that the node owns no slots and calls disconnect() on it, i.e. on the connection running the loop.
  3. disconnect() frees the bufferevent and empties the pipeline: with --retry-on-error the requests go to the replay queue (handed off by Resend held retries when a slot map refresh retires a connection #575), otherwise they are uncounted.
  4. Back in the loop, parse_response() returns the replies still buffered behind the CLUSTER SLOTS reply (the resends of --retry-on-error follow it on that connection), and pop_req() pops an empty pipeline. Debug builds hit the assertion. Release builds use a garbage request*.

The non-retry variant comes from the same retirement but by another route. Producers parked by hold_pipeline() (in-flight budget) wait on the retired connection's pooled and in-flight work. handle_cluster_slots() drops that work, but no reply is left to make a parked producer run fill_pipeline() again. In gdb, every live connection of both clients was paused with nothing pending and generated == processed, so the event loop ran out of events.

Fix

  • shard_connection keeps a disconnect counter that disconnect() bumps. process_response() notes it before parsing and stops once a handler has torn the connection down. It is a counter rather than a state check so that a disconnect followed by a reconnect inside the same handler is caught too. The remaining buffered replies have no request left to match: disconnect() already requeued or uncounted each one. The rest of process_response() runs as after any loop exit (see the review reply for why that is safe).
  • handle_cluster_slots() wakes every live connection with schedule_fill() whenever it retires a connection, as it already did for --multi-key-get. "Retires" includes a connection that was already disconnected, and requests that hand_off_replay_requests() drops: everything dropped there frees --requests and in-flight budget.

The #575 replay hand-off (hand_off_replay_requests() / adopt_replay_request()) and the monitor and key-pool compensation in the retirement loop are not changed. The generated/processed accounting is unchanged too.

--test-time runs are fixed as well. Locally, master aborted 2/3 runs of a 12 s run with a slot move at 3 s; with this PR all 3 ran the full 12 s.

Stats consequence: replies dropped after a self-teardown are not counted. The server did execute those commands, but memtier never matches their replies. With --retry-on-error they are resent and counted once, on their final reply. Without it they are uncounted from the generated total, like any other request dropped by disconnect(). Stats can therefore under-report work the server did around a retirement, by up to one pipeline per retired connection.

Tests

tests/test_cluster_failover_retry.py gets two tests:

  • What both do: move every slot of a non-seed primary to the seed primary and back, six times, during a --requests run with --pipeline 16. They then check rc, stderr and the exact op count.
  • How the move is done: DELSLOTSRANGE on every node, old owner first, then ADDSLOTSRANGE + BUMPEPOCH on the new owner. If gossip handed the slots back in between, ADDSLOTSRANGE fails with "already busy"; the helper then deletes again and retries.
  • Variants: one test runs with --retry-on-error, the other without.
  • Where they run: they need no replicas, so they run in the plain OSS-cluster cells as well as in the replicas cell that already runs this file. The module docstring now covers both groups of tests.

Measured locally (RLTest, 3 shards, Redis 8.6.0, rebased on e4d14e8). Each cell is failed runs / runs, for the --retry-on-error test then the no-retry test:

binary plain cluster cluster + replicas
master e4d14e8 6/6 aborted (pop_req assertion), 6/6 stopped early 3/3, 3/3
this PR without the read-loop break 6/6 aborted, 0/6
this PR 0/22, 0/22 0/6, 0/6 (plus the #568 failover test 0/6)

Notes on these numbers:

  • The memtier run outlives all six moves: with the PR, memtier was still running after all 6 moves in 43 of 44 plain runs (5 moves in the other).
  • The "already busy" race reviewers hit (about 1 in 10–20 runs) did not happen in these runs, nor in 160 back-to-back moves of a standalone stress loop of the same helper. The retry path is there for CI hosts where it does.

Each half of the fix is needed. Without the break the retry test aborts every time. With only the break (the earlier revision), the no-retry test still failed 1/3.

Standalone repro from the issue (3 primaries + 1 replica, -n 2000000 --pipeline=16):

  • --retry-on-error: master fa45424 aborted 3/4; this PR failed 0/4.
  • no retry: master stopped early 3/4; this PR failed 0/4.

Existing suites on this revision, all passing:

  • cluster: test_retry_on_error.py, test_reconnections.py, test_cluster_transaction.py, test_transaction_pipeline.py, test_mget_cluster.py, test_monitor_cluster.py, test_monitor_cluster_backpressure.py (52 tests)
  • cluster + replicas: test_read_preference_failover.py, test_read_preference_transaction.py
  • standalone: test_retry_on_error.py, test_reconnections.py (18 tests)

The previous revision also passed tests_oss_simple_flow.py.

Fixes #578

Affected versions

2.4.0 through 2.4.4: confirmed by running. Master: confirmed by running. 2.5.0 through 2.5.2: the code path is present, but the crash wasn't reproduced because another bug masks it.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster topology retirement and the hot process_response() loop; incorrect handling could still mis-account stats or stall producers, though scope is narrow and covered by new integration tests.

Overview
Fixes cluster-mode runs that abort or stop early when a CLUSTER SLOTS refresh retires the same connection that is handling that reply (e.g. all slots moved off a live primary after -MOVED).

shard_connection increments m_disconnect_count on disconnect() and process_response() stops the parse loop once a handler tears the connection down, so leftover buffered replies are not paired with an emptied pipeline (avoids the pop_req() assertion with --retry-on-error).

handle_cluster_slots() tracks whether any connection was retired and schedule_fill() on all live shards when that happens (not only for --multi-key-get), so producers parked on in-flight budget wake up and --requests can finish without retry.

Tests: test_cluster_failover_retry.py adds mid-run slot move cycles with pipeline depth, with and without --retry-on-error.

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

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. The root-cause write-up is easy to follow, and the table showing each half of the fix failing on its own is the kind of evidence that makes this easy to review. I went through the diff and have a couple of small questions, nothing blocking.

  • In process_response() the new break leaves the loop on a connection that disconnect() has already torn down, and then still runs the tail: the reconnect_interval block and fill_pipeline(). I see the description points to an earlier reply on why that's safe, and as far as I can tell disconnect() marks the setup ladder as setup_done, so fill_pipeline() won't try to send setup commands on a NULL m_bev. Just checking: is the reason for break rather than return that the finished() / set_end_time() check at the bottom still needs to run? If so, I think a short note next to the break would help the next person who wonders why we keep going on a dead connection.
  • I may be wrong here, but the counter check also fires for any other handler that calls disconnect() or attempt_reconnect() from the default arm, not just the CLUSTER SLOTS retirement. That seems like the right behaviour (the remaining buffered bytes belong to the old socket either way). Was that intended? If so, the comment could say "any handler" rather than only giving the CLUSTER SLOTS example.
  • Nitpicking: _slot_ranges() re-parses CLUSTER NODES much like _cluster_nodes() does just above it. I wonder if _cluster_nodes() could return the slot tokens too, so there's one parser.

The test pins the exact scenario from #578, covers both the retry and no-retry variants, and is shown to fail on master, so it clears the regression-test bar this repo has set before (e.g. #364). The stats under-count around a retirement is disclosed and seems reasonable. Since this is cluster routing, a second look from whoever owns the cluster client would be good. The core fix looks solid to me.

(would file this as a comment, not a formal block)

…s it

When every slot moves off a primary mid-run, that primary's connection
answers -MOVED, sends CLUSTER SLOTS itself and, handling the reply in its
own process_response(), handle_cluster_slots() retires it: disconnect()
frees the bufferevent and empties the pipeline. The loop then went on
parsing replies still buffered behind the CLUSTER SLOTS reply and popped
an empty pipeline. With --retry-on-error (whose resends follow the
CLUSTER SLOTS request on that connection) this aborted with
"Assertion m_pending_resp >= 0" in pop_req(); release builds use a
garbage request.

process_response() now notes a per-connection disconnect counter before
parsing and stops once a handler has torn the connection down.
disconnect() has already requeued (--retry-on-error, handed off by #575)
or uncounted every request those replies answered.

Without --retry-on-error the same retirement ended runs with "client(s)
stopped before reaching their stop condition": producers parked by
hold_pipeline() waited on in-flight work of the retired connection, which
disconnect() dropped without waking anyone, so the event loop ran out of
events. handle_cluster_slots() now wakes every live connection whenever
it retires a connection (dropping its staged commands, key pool, pipeline
or unplaceable replays), as it already did for --multi-key-get.

tests/test_cluster_failover_retry.py moves every slot of a primary to
another primary (and back) six times during a --requests run, with and
without --retry-on-error, and checks the exact op count. On master the
first test aborts and the second stops early in every run; both pass with
this change.

Fixes #578

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/cluster-self-retire-abort branch from 583f624 to dc4daf5 Compare October 13, 2026 22:06
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Taking your points in order (fixes are in dc4daf5):

break vs return in process_response(). The fall-through is deliberate.

  • The --reconnect-interval block. It can't run here. Cluster mode rejects the option at startup (memtier_benchmark.cpp: "error: cluster mode dose not support reconnect-interval option."). So with --cluster-mode that block is unreachable, after the break or otherwise. Outside cluster mode nothing retires a connection from inside its own read loop.
  • fill_pipeline() on the retired connection. It is a no-op:
    • The replay drain is gated on conn_connected.
    • is_conn_setup_done() is true after disconnect().
    • cluster_client::hold_pipeline() returns true at once for a disconnected connection. If that connection held the --transaction pin, the same check also releases the pin and wakes the other connections.
    • The idle check at the end is skipped because m_bev is NULL.
  • Why not return. The final finished() → set_end_time() check runs as after any loop exit. That is the reason to fall through rather than return.

Wake-up vs the --transaction pin release. The two don't conflict:

  • The sweep only arms the zero-delay fill timer on live connections.
  • A non-pin connection woken while a live pin holds the rotation goes through hold_pipeline(), gets true, and pauses again.
  • If the pin itself was the retired connection, hold_pipeline() releases it on the next fill_pipeline() call: either the woken peers' call, or the retired connection's own call after the break.
  • The sweep now also fires for a connection that was already disconnected, and when hand_off_replay_requests() drops replays. Both free --requests budget without waking anyone.

Module docstring. Updated. It now covers both groups of tests and says the #578 slot-move tests need no replicas, with the OSS_CLUSTER=1 command line to run them.

Leftover slot layout / cluster-node-timeout.

  • RLTest doesn't reuse environments by default (--env-reuse is off), so each test gets a fresh cluster, and CI doesn't pass the flag.
  • The test now moves the slots six times: off the victim, then back, and so on, ending off it.
  • _move_slots() also handles a race other reviewers hit. Gossip could hand the slots back to the old owner between DELSLOTSRANGE and ADDSLOTSRANGE, failing the latter with "already busy". The helper now deletes on the old owner first and retries after deleting again.

The 5/5 figure. Replaced with measured rates. Each pair is failed runs / runs for the --retry-on-error test and the no-retry test:

  • master e4d14e8: 6/6 and 6/6 (plain cluster), 3/3 and 3/3 (with replicas).
  • This PR: 0/22 and 0/22 (plain cluster), 0/6 and 0/6 (with replicas).

🤖 Generated with Claude Code

The last cycle of the #578 slot-move tests left the victim's slots on the
seed primary. With --retry-on-error over TLS that occasionally (about 2%
of runs) trips an unrelated, pre-existing redirect livelock and hangs the
run until the test's timeout; it is tracked separately. Every cycle now
retires the victim's connection, waits a second and then returns the
slots, so the run still exercises the self-retire but ends on the
original layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Addressed the build reviewer's TLS hang in 30deb73. The --retry-on-error slot-move test now hands the victim's slots back after every cycle, the last one included, so the run ends on the original layout. Each cycle still waits a second after the move so memtier retires the victim's connection (the #578 path).

The hang itself is a separate, pre-existing livelock: a connection holding more than --pipeline requests when -MOVED arrives never sends CLUSTER SLOTS, and its stale map keeps retrying it. It is tracked separately in #TBD and is not fixed here.

Local runs (failed / valid runs, RLTest, 3 shards):

  • This PR over TLS: 31 runs of both tests, no failures and no hang.
  • Master e4d14e8 over TLS: the retry test aborted 4/4.
  • No-break mutant over TLS: the retry test aborted 4/4.

🤖 Generated with Claude Code

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

The separate redirect livelock that the final slot-move cycle avoids is tracked in #590, and its fix is in PR #591. #591 should merge after this PR (see the interaction review on #591).

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit a388a86 into master Oct 14, 2026
127 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
fill_pipeline() only ran its loop while the run was not finished. With
--test-time, a MOVED that arrives after the deadline still asks for a
CLUSTER SLOTS refresh, but the loop never ran, so the refresh was never
sent and the retries in flight kept bouncing on MOVED forever, just as
with a full pipeline. Once finished, keep running the loop for setup
commands while responses are pending, and stop it before any new
request is created.

Tests:
- test_moved_after_test_time_refreshes_slots: MOVED starts after
  --test-time=1 is up. It hung on the previous commit.
- test_pipeline_depth_stays_at_cap: a batching peer checks that each
  connection holds exactly --pipeline requests, which catches a
  "size() > pipeline" off-by-one in the moved depth check.
- The overfull-connection test also runs a burst variant, where the
  node answers CLUSTER SLOTS and everything queued behind it in one
  write (needs #584). It now checks Totals Count == --requests and
  bounds the MOVED count.

Refs #590

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
fill_pipeline() only ran its loop while the run was not finished. With
--test-time, a MOVED that arrives after the deadline still asks for a
CLUSTER SLOTS refresh, but the loop never ran, so the refresh was never
sent and the retries in flight kept bouncing on MOVED forever, just as
with a full pipeline. Once finished, keep running the loop for setup
commands while responses are pending, and stop it before any new
request is created.

Tests:
- test_moved_after_test_time_refreshes_slots: MOVED starts after
  --test-time=1 is up. It hung on the previous commit.
- test_pipeline_depth_stays_at_cap: a batching peer checks that each
  connection holds exactly --pipeline requests, which catches a
  "size() > pipeline" off-by-one in the moved depth check.
- The overfull-connection test also runs a burst variant, where the
  node answers CLUSTER SLOTS and everything queued behind it in one
  write (needs #584). It now checks Totals Count == --requests and
  bounds the MOVED count.

Refs #590

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
fill_pipeline() only ran its loop while the run was not finished. With
--test-time, a MOVED that arrives after the deadline still asks for a
CLUSTER SLOTS refresh, but the loop never ran, so the refresh was never
sent and the retries in flight kept bouncing on MOVED forever, just as
with a full pipeline. Once finished, keep running the loop for setup
commands while responses are pending, and stop it before any new
request is created.

Tests:
- test_moved_after_test_time_refreshes_slots: MOVED starts after
  --test-time=1 is up. It hung on the previous commit.
- test_pipeline_depth_stays_at_cap: a batching peer checks that each
  connection holds exactly --pipeline requests, which catches a
  "size() > pipeline" off-by-one in the moved depth check.
- The overfull-connection test also runs a burst variant, where the
  node answers CLUSTER SLOTS and everything queued behind it in one
  write (needs #584). It now checks Totals Count == --requests and
  bounds the MOVED count.

Refs #590

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 --retry-on-error: a connection retiring itself inside process_response() aborts in pop_req()

1 participant