Skip to content

Don't send a second CLUSTER SLOTS while one is in flight - #618

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/overlapping-cluster-slots-refresh
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/overlapping-cluster-slots-refresh

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --cluster-mode and a --read-preference that routes to replicas, a connection can have two CLUSTER SLOTS requests in flight at once. The second reply fails ("cluster slot failed."), the connection stops reading, and a --requests run hangs with no exit and no supervisor abort. Repro: a no-shebang write script marked --command-is-read, so replicas answer -READONLY. On master a388a86 (where it was first measured; the PR is now rebased onto 013cb85) it hung 3/12 runs. Details are in #617.

Root cause

  • set_cluster_slots() set m_cluster_slots = setup_none unconditionally, even while a refresh was setup_sent. The next fill_pipeline() then sent a second CLUSTER SLOTS.
  • The first reply turns off set_keep_value (shard_connection.cpp:1249). The second reply is then parsed without its value, logs "cluster slot failed.", and sets error = true.
  • On error, process_response() returns with complete replies still in the input buffer. No more bytes arrive, so nothing reads them.
  • The -READONLY handler (cluster_client.cpp:3081) is the caller that does this. With no live primary to reroute to, it requests a refresh on the rejecting replica connection. That covers every request when --retry-on-error is off, since requests carry no key then.
  • handle_moved() and cluster_client::reconnect() already skip the call unless the state is setup_done. The -READONLY path did not.

Fix

set_cluster_slots() is now a no-op while a refresh is setup_sent. The reply already on its way updates the slot map anyway. disconnect() resets the state to setup_done, so a reconnect still sends a new CLUSTER SLOTS.

I checked every caller:

The guard is in the setter, so any new caller is covered too.

A second commit fixes the comment in the rt_cluster_slots reply arm. When a refresh reply is rejected, the state stays setup_sent, and that comment said other paths could re-arm it. With this change only disconnect() re-arms it.

Interaction with open PRs

Tests

New file: tests/test_cluster_slots_refresh_overlap.py. Two scripted RESP peers act as a primary and a replica. The replica answers the first two GETs with -READONLY and pauses 0.3 s after each, so the second arrives while the first refresh is still unanswered. The test asserts:

  • exit code 0;
  • no "cluster slot failed";
  • exactly one CLUSTER SLOTS sent to the replica.

Measured locally:

Binary New test Real-cluster repro (timeout 30)
origin/master a388a86 0/10 pass (timeout, "cluster slot failed", 2 refreshes) 3/12 hung; 2–64 "cluster slot failed" per run
this PR 30/30 pass (3/3 again after rebasing onto 013cb85) 0/20 hung; 0 "cluster slot failed"

The test's sleep(0.3) pause was stress-tested in review. master never passed falsely under nice 19 with busy loops, with SIGSTOP pauses, or with the pause removed. A slow runner could only make master pass falsely; it can't make the fix fail, because the replica answers the second GET before the queued CLUSTER SLOTS. A fully deterministic variant (the peer waits for the refresh instead of sleeping) can be a follow-up.

test_read_preference_modes.py, test_read_preference_failover.py, test_cluster_redirect_stats.py, test_retry_on_error.py and the new test pass on a local 3+3 oss-cluster (21/21).

Affected versions

2.5.0 through 2.5.2 (latest) and master. ee757e7 (#456, --read-preference, first tag 2.5.0) introduced it. Confirmed by running on master, 2.5.2 (4/6 hung) and 2.5.0 (5/6 hung). 2.5.1 is derived from history.

Fixes #617

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster-mode topology refresh timing on error paths (-READONLY); scope is narrow but affects connection setup and routing correctness under read preference.

Overview
Prevents overlapping CLUSTER SLOTS refreshes on a single shard connection, which could hang cluster-mode runs when replica -READONLY handling triggered a second refresh before the first reply arrived.

set_cluster_slots() now only moves m_cluster_slots to setup_none when it is not already setup_sent, so a refresh already on the wire is not duplicated. The in-flight reply still updates topology; disconnect() resets state so reconnects can send a fresh refresh. Comments in shard_connection.cpp note that while state stays setup_sent, only disconnect re-arms a new refresh.

Adds tests/test_cluster_slots_refresh_overlap.py: scripted primary/replica peers, --read-preference=secondary, two staggered -READONLY responses, and asserts exit 0, no "cluster slot failed", and exactly one CLUSTER SLOTS on the replica.

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

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

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

Thanks for this, and for the long write-up. The caller-by-caller walk made it easy to check. I looked at all five set_cluster_slots() call sites and agree with the reasoning. Putting the guard in the setter is a better choice than adding another == setup_done check at the -READONLY site, because it also covers any caller added later. A few questions, none of them blocking:

  • The new comment at shard_connection.cpp:1241 says only disconnect() re-arms a rejected reply now. I think that matches what really happened before too: a connection stuck at setup_sent from bootstrap never reaches setup_done, so -READONLY and the cross-shard trigger couldn't run on it anyway. Can you confirm nothing in practice relied on the old "other paths can re-arm" wording?

  • Just checking: with this change, a refresh asked for while one is in flight gets dropped, not queued. If the in-flight reply was produced before whatever caused the second request (for example a MOVED seen on another connection), we keep a slightly stale map until the next redirect causes a new refresh. I guess that heals itself and is far better than hanging, but it may be worth saying so in the setter comment.

  • The deeper hazard is still there: on error, process_response() returns at line 1411 with complete replies still in the input buffer, and nothing reads them again. This PR removes the one trigger we know about. Is every other error = true path (AUTH/SELECT/HELLO/READONLY rejections) guaranteed to end in a disconnect, or could one of them stall the same way? A follow-up issue may be enough here. Whoever owns the cluster connection lifecycle may want to take a second look at this one.

  • On the test: if both -READONLY replies land in the same read, master would also send only one CLUSTER SLOTS. So the 0.3 s pause only guards against a false pass, not a flaky failure, which is the right way round. Your 0/10 on master shows it really fails on the old code, which is the bar we want.

The core fix looks right to me, and the regression test exercises the exact broken sequence.

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

fcostaoliveira and others added 2 commits October 14, 2026 08:01
set_cluster_slots() reset the refresh state to setup_none even when a
CLUSTER SLOTS was already setup_sent, so the next fill_pipeline() sent a
second one. The first reply turns off keeping the reply value, the
second is then parsed without it and fails ("cluster slot failed."), and
process_response() returns with complete replies still in the input
buffer. Nothing reads them again, and a --requests run hangs.

The -READONLY handler hits this: with no live primary to reroute to (for
example every request when --retry-on-error is off, as requests carry no
key then), each -READONLY asks the rejecting replica connection for a
refresh. handle_moved() and cluster_client::reconnect() already skip the
call unless the state is setup_done; the -READONLY path did not.

Make set_cluster_slots() a no-op while a refresh is in flight. The reply
on its way updates the slot map anyway, and disconnect() resets the
state, so a reconnect still sends a new CLUSTER SLOTS.

The test uses two scripted peers, a primary and a replica. The replica
answers the first two GETs with -READONLY, pausing after each, so the
second arrives while the refresh from the first is in flight. On master
memtier sends a second CLUSTER SLOTS and hangs.

Fixes #617

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With set_cluster_slots() a no-op while a refresh is setup_sent, a later
-READONLY no longer re-arms a connection whose refresh reply was
rejected; only disconnect() does. Update the comment that promised other
paths could.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/overlapping-cluster-slots-refresh branch from 62423ab to 3422f0b Compare October 14, 2026 07:02
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Answers point by point:

1. Is a disconnect now the only way out of a rejected reply? Yes. After a rejected reply the state stays setup_sent, and only disconnect() resets it (to setup_done). That can come from a --reconnect-on-error reconnect, from the peer closing the socket, or from the run ending. Before this PR the only other way out was a later -READONLY on a request still in flight. A MOVED could never do it, because handle_moved() already returned unless the state was setup_done. Re-arming from -READONLY meant resending CLUSTER SLOTS to a server that had just sent a malformed reply. It also left the overlapping-refresh hazard this PR removes.

One correction to the reasoning: report_connection_stage_failure() only helps before steady state. Once any connection has had a successful response, it returns early (memtier_benchmark.cpp:209). So during bootstrap the supervisor still bounds a rejected reply. Mid-run, recovery depends on a disconnect, as described above. The test fixture behind that comment closes the socket after the bad reply, so it is unaffected. I pushed 3422f0b to fix the comment so it no longer promises a re-arm that can't happen.

2. Nit: mention that the handle_moved()/reconnect() checks are now redundant. Agreed that they are redundant but harmless. I left them, and the setter comment, as they are to keep the diff minimal. Each check also documents locally why that call site can't send a second refresh. They can be dropped in a follow-up cleanup if maintainers prefer.

3. Test determinism. Your analysis matches what we measured. A slow runner can only make master pass falsely, and master never did: the review ran it under nice 19 with busy loops, with SIGSTOP pauses, and with the pause removed. After rebasing onto 013cb85 the test passed 3/3 on the fix.

🤖 Generated with Claude Code

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 --read-preference: a second CLUSTER SLOTS sent while one is in flight wedges the connection; run hangs

1 participant