Repository navigation
Don't send a second CLUSTER SLOTS while one is in flight - #618
Conversation
|
🤖 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
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) |
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]>
62423ab to
3422f0b
Compare
|
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 One correction to the reasoning: 2. Nit: mention that the 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 🤖 Generated with Claude Code |
Problem
With
--cluster-modeand a--read-preferencethat 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--requestsrun 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()setm_cluster_slots = setup_noneunconditionally, even while a refresh wassetup_sent. The nextfill_pipeline()then sent a second CLUSTER SLOTS.set_keep_value(shard_connection.cpp:1249). The second reply is then parsed without its value, logs "cluster slot failed.", and setserror = true.error,process_response()returns with complete replies still in the input buffer. No more bytes arrive, so nothing reads them.-READONLYhandler (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-erroris off, since requests carry no key then.handle_moved()andcluster_client::reconnect()already skip the call unless the state issetup_done. The-READONLYpath did not.Fix
set_cluster_slots()is now a no-op while a refresh issetup_sent. The reply already on its way updates the slot map anyway.disconnect()resets the state tosetup_done, so a reconnect still sends a new CLUSTER SLOTS.I checked every caller:
connect(): the main connection is disconnected or new, so its state issetup_done.reconnect()andhandle_moved(): already guarded. Their behaviour is unchanged.create_request(): runs only once the producer is set up.-READONLY: the fixed path.setup_nonerefresh, which this change does not touch.The guard is in the setter, so any new caller is covered too.
A second commit fixes the comment in the
rt_cluster_slotsreply arm. When a refresh reply is rejected, the state stayssetup_sent, and that comment said other paths could re-arm it. With this change onlydisconnect()re-arms it.Interaction with open PRs
-READONLYdrops as ops): touches the same handler for accounting only. No conflict, and the two are independent.--failed-keys-file): keeps the refresh-on--READONLYfallback when--retry-on-erroris off and addsp != conn_id. The new test runs without--retry-on-error, so it still goes through the refresh path with Record the key of --command requests in --failed-keys-file #599 applied. No textual conflict.rt_cluster_slotsreply arm inshard_connection.cpp. This PR changes onlyshard_connection.h:174. The two complement each other: with this fix there are fewer and never overlapping refreshes, and Wake peers when a replica finishes a CLUSTER SLOTS refresh #605 wakes peers when each one ends.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-READONLYand pauses 0.3 s after each, so the second arrives while the first refresh is still unanswered. The test asserts:Measured locally:
timeout 30)The test's
sleep(0.3)pause was stress-tested in review. master never passed falsely undernice 19with 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.pyand the new test pass on a local 3+3oss-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 movesm_cluster_slotstosetup_nonewhen it is not alreadysetup_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 inshard_connection.cppnote that while state stayssetup_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.