Skip to content

Never send CLUSTER SLOTS inside an open --transaction MULTI (#569) - #576

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix/cluster-slots-inside-multi
Oct 13, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix/cluster-slots-inside-multi

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #569

Problem

--cluster-mode --transaction runs can hang past --test-time when connections get dropped (for example CLIENT KILL TYPE normal on one node). memtier logs "pin connection (id=N) received MOVED mid-rotation", then "cluster slot failed", and never exits.

Root cause

A CLUSTER SLOTS refresh was written on the pinned transaction connection between MULTI and EXEC. The server queues it and answers +QUEUED. The CLUSTER SLOTS reply handler treats that as a failed refresh, so m_cluster_slots never reaches setup_done, fill_pipeline() keeps waiting for setup, and the run wedges. There are two ways in:

  1. MOVED/ASK mid-MULTI on the pin. While a shard is disconnected, the rotation falls back to the current connection with a key it doesn't own. The keyed command gets -MOVED, and handle_moved() re-arms CLUSTER SLOTS on that same connection, which still has the MULTI open.
  2. Reconnect only the dropped cluster connection, and only once #560's MAIN_CONNECTION->set_cluster_slots() in cluster_client::reconnect(). An unrelated shard drops while the main connection is pinned inside a MULTI.

On the MOVED path, the pin was also released mid-rotation, so the rotation's EXEC went to a different shard ("EXEC without MULTI") and the MULTI on the original connection stayed open.

Fix

  • shard_connection tracks m_multi_open. It is set when a --transaction MULTI is written, and cleared on EXEC/DISCARD and on connect/disconnect.
  • fill_pipeline() defers a pending CLUSTER SLOTS refresh while the connection still drives the pinned rotation inside an open MULTI (connections_manager::is_transaction_pinned()). The refresh goes out right after the rotation's EXEC/DISCARD is written.
  • A MOVED/ASK on the pin connection inside an open MULTI no longer releases the pin. EXEC still reaches the connection that holds the MULTI, and the server answers EXECABORT: the interrupted transaction is aborted atomically on one connection, not split across two. Outside a MULTI the pin is released as before.
  • Abandoned MULTI: a rotation that opens a MULTI and never closes it (e.g. --command=MULTI --command="SET __key__ __data__", no EXEC) leaves that MULTI open after the rotation wraps and pins another shard. If a refresh is then due on that connection, memtier sends an internal DISCARD (new setup-only request type rt_discard) before CLUSTER SLOTS. This path is reachable and has its own test; without it that workload hangs. rt_discard is excluded from workload stats and read classification. A well-formed MULTI ... EXEC rotation never reaches it.
  • Each --command's MULTI/EXEC/DISCARD role is resolved once at config time (arbitrary_command::multi_effect), so the request path does no string compares.

Test evidence

New test: test_cluster_transaction.py::test_transaction_connection_kills_do_not_hang. It runs MULTI/SET/EXEC with --transaction -t1 -c1 --pipeline=1 --test-time=8 --reconnect-on-error while it repeatedly runs CLIENT KILL TYPE normal on a non-main shard. It asserts that the run:

  • exits 0 within --test-time + 30s;
  • never logs "cluster slot failed";
  • actually provoked a MOVED mid-rotation, so the test can't pass without exercising the path.
Build Regression test (RLTest, 3-shard OSS cluster) Manual repro (timeout 40, 12 kills)
master 667f187 0/5 pass: all hung past --test-time 5/5 hung (exit 124, "cluster slot failed")
this PR 10/10 pass 13/13 exit 0 (0 "cluster slot failed")

With this PR, every MOVED inside a MULTI now yields exactly one EXECABORT on the same connection (for example 11821 MOVED → 11821 EXECABORT). The only "EXEC without MULTI" errors left (0–5 per run) come from the pinned connection itself being killed mid-rotation, an existing path this PR doesn't change.

New test: test_cluster_transaction.py::test_transaction_abandoned_multi_is_discarded_before_refresh. It uses the same kill workload with a MULTI/SET rotation (no EXEC) and asserts a clean exit, no "cluster slot failed", and that the servers saw DISCARD (INFO commandstats). It passed 3/3 on this branch. With the DISCARD fallback compiled out it failed 2/2 (hung past --test-time, "cluster slot failed"). The kill test above also asserts 0 DISCARDs for the well-formed MULTI/SET/EXEC rotation.

Also run on the head commit, all passing: test_cluster_transaction.py (16/16, twice), test_transaction_pipeline.py (3/3). Earlier revision: test_reconnections.py (9/9).

Not fixed here (pre-existing, separate issue)

With --pipeline > 1 the same kill workload can crash a worker with vector::_M_range_check in client::create_arbitrary_request on master as well. txn_release_pin() pushes a lone staged key index into the old pin's m_key_index_pools, and cluster_client::create_request() later pops it as a command index. This PR makes that path rarer (MOVED inside a MULTI no longer releases the pin) but doesn't fix it. It will be filed separately.

Affected versions

Affected versions: 2.4.0–2.5.2 and master (introduced by f7f8983 / #390; widened by #560 / 6f15854); hang confirmed on 2.5.2 and 2.4.0

🤖 Generated with Claude Code

In --cluster-mode --transaction, a slot map refresh could be written on the
pinned connection between MULTI and EXEC: after a MOVED/ASK mid-rotation
(handle_moved re-arms CLUSTER SLOTS on that connection), or when
cluster_client::reconnect() requests a refresh on the main connection while
it holds the pin. The server queues it and answers +QUEUED, the reply handler
reports "cluster slot failed", m_cluster_slots never reaches setup_done and
the run hangs past --test-time.

- shard_connection tracks m_multi_open, set when a MULTI is written and
  cleared on EXEC/DISCARD or (re)connect.
- fill_pipeline() defers a pending CLUSTER SLOTS refresh while the
  connection still drives the pinned rotation inside an open MULTI; it is
  sent right after the rotation's EXEC/DISCARD.
- A MOVED/ASK on the pin connection inside an open MULTI no longer releases
  the pin: the rest of the rotation (EXEC) still goes to the connection
  holding the MULTI and the server answers EXECABORT, instead of EXEC being
  sent to another shard ("EXEC without MULTI") and this MULTI left open.
- Defensively, if a refresh is due on a connection whose MULTI was
  abandoned by its rotation, a DISCARD is sent before CLUSTER SLOTS.

Adds a regression test that kills one shard's connections during a
--transaction run; it hung on master 5/5 and passes 10/10 with the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 clear, and both new tests are the kind we want: the first hangs on master and passes here, and the second actually exercises the DISCARD fallback instead of just claiming it can be reached. Moving the MULTI/EXEC/DISCARD role into resolve_command_meta() also keeps the string compares off the per-request path, which is good. A few questions below:

  1. m_multi_open is set and cleared when requests are written, but the MOVED/ASK handlers read it when the reply comes back. With --pipeline=1 those are the same rotation. With --pipeline > 1, I think the flag can already belong to the next rotation's MULTI by the time the MOVED for the previous rotation's SET is processed. The reverse can also happen: EXEC is already written, so the flag is false, and the pin gets released while the server still has the MULTI open. I know the PR scopes pipeline > 1 out, and that path already has a separate crash on master. I just want to check that this PR doesn't make the pin decision on that path any less predictable than before. Did you consider tracking open/closed per in-flight request instead, or is that more trouble than it's worth?

  2. The DISCARD fallback adds a new internal request type only to handle a rotation that opens a MULTI and never closes it. Would it be simpler to reject that rotation when --transaction is set, at config time where multi_effect is already resolved? Then rt_discard wouldn't be needed. I may be missing a valid use case for an unclosed MULTI rotation, though. If there is one, the current approach seems fine.

  3. Nitpicking: the "keep the pin inside MULTI" block, comment included, is now copied identically into the MOVED and ASK handlers. A small helper would keep the two paths from drifting apart.

  4. The first test needs "received MOVED mid-rotation" in stderr. Is that message logged without --debug? And since the test depends on timing (an 8s run with kills every 0.5s), have you seen it reliably provoke a MOVED on slower CI runners? If it doesn't, the test fails because nothing was exercised, not because of a regression. That's still better than passing silently, but it could get flaky.

Cluster transaction pinning is subtle enough that this may also be worth a second look from whoever knows the cluster routing code best. The fix itself looks solid to me. I'd mainly like an answer on (1) before this merges.

fcostaoliveira and others added 2 commits October 13, 2026 17:54
send_arbitrary_command_end() compared command_type against "MULTI",
"EXEC" and "DISCARD" on every --transaction request. The role is static
per --command, so resolve_command_meta() now records it from argv[0] in
arbitrary_command::multi_effect and the request path only checks an enum.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The DISCARD sent before CLUSTER SLOTS is reachable: a --transaction
rotation that opens a MULTI and never closes it (--command=MULTI
--command="SET __key__ __data__", no EXEC) leaves the MULTI open once the
rotation wraps and pins another shard, and the next refresh on that
connection would otherwise be queued (+QUEUED) and hang the run.

Add test_transaction_abandoned_multi_is_discarded_before_refresh, which
kills a non-main shard's connections under that rotation and checks the
run exits cleanly and the servers saw DISCARD. Without the fallback it
hangs past --test-time. The existing kill test now also asserts that a
well-formed MULTI/SET/EXEC rotation never needs the DISCARD. Spell out in
the code comment when the fallback runs.

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

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. I pushed two commits: f7c9e18 (MULTI/EXEC/DISCARD role resolved at config time) and 828e8a8 (test for the DISCARD fallback, plus a clearer code comment). I didn't rebase: the branch still merges cleanly on top of #571, #572 and #573.

1. m_multi_open is write-side state, so at --pipeline > 1 a MOVED can belong to an earlier rotation

Yes, and keeping the pin is still right there. The pin decides where future writes go, so write-side state is the right input. Take rotation N (MULTI/SET/EXEC), all written on connection A. Rotation N+1 has also pinned A and written its MULTI. Then the MOVED for N's SET arrives:

  • N's EXEC is already on A's wire, so the server answers it with EXECABORT. Nothing about the pin can change that.
  • is_multi_open() is true because of N+1's MULTI. That MULTI is on A, so N+1's remaining commands and its EXEC have to go to A too. Releasing the pin here would send them to another connection: "EXEC without MULTI" there, and a MULTI left open on A. That is the Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569 split again, just one rotation later.
  • The refresh armed by that MOVED is deferred until N+1's EXEC is written, then goes out as the first thing after it.
  • If the MOVED lands between rotations (N's EXEC written, N+1's MULTI not yet), is_multi_open() is false and the pin is released as before. The next rotation re-pins from scratch, so nothing gets split.
  • If N+1 pinned a different connection, A is no longer the pin, so the MOVED takes the existing "stale (non-pin) connection" branch and is dropped.

A --pipeline=2 variant of the kill test is the right way to pin this down. Right now it's blocked by the separate vector::_M_range_check crash in client::create_arbitrary_request (a lone staged key index pushed back by txn_release_pin() is later popped as a command index). That crash also reproduces on master, and I'm filing it as its own issue now. Once it's fixed, I'll add the --pipeline=2 variant with that fix.

2. Can the defensive DISCARD (rt_discard) be reached today?

Yes, so I added a test. Here's the path: a --transaction rotation that opens a MULTI and never closes it, for example --command=MULTI --command="SET __key__ __data__" with no EXEC.

  1. The rotation writes MULTI, then SET, on its pin, so that connection's m_multi_open stays true.
  2. The rotation wraps and pins whichever shard owns the next key.
  3. A refresh gets armed on the old connection. Two ways: a shard reconnect re-arms the main connection, or a MOVED arrives on it.
  4. That connection is no longer the pin, so the fill_pipeline() deferral doesn't apply and send_conn_setup_commands() runs with the MULTI still open.

Without the DISCARD, CLUSTER SLOTS is queued (+QUEUED), "cluster slot failed" is logged, and the run hangs.

Evidence on a local 3-shard cluster, with the kill loop from the existing test running against a non-main shard:

  • With the fallback, manual repro: 3/3 exit 0, 0 "cluster slot failed", and 1.5k–5.8k cmdstat_discard calls on the main node.
  • With the DISCARD block compiled out, same repro: 3/3 hung (timeout 40 exit 124, "cluster slot failed").

New test test_transaction_abandoned_multi_is_discarded_before_refresh (828e8a8) runs that workload. It asserts a clean exit, no "cluster slot failed", and DISCARD calls > 0 across the masters (memtier is the only client sending DISCARD). Results: 3/3 pass on this branch, 2/2 fail (hang) with the fallback compiled out.

The existing kill test now also asserts 0 DISCARDs for MULTI/SET/EXEC. So a well-formed rotation never needs the fallback: there, the refresh just waits for the EXEC. I rewrote the code comment to say exactly when the fallback runs.

3. Per-request string compares in send_arbitrary_command_end()

Done in f7c9e18. resolve_command_meta() now resolves each --command's role once from argv[0], case-insensitively, into arbitrary_command::multi_effect (opens / closes / none). The request path only checks that enum. Using argv[0] also makes it independent of command_type, which the monitor-expansion path rewrites.

Tests (head 828e8a8, local 3-shard OSS cluster via RLTest)

  • test_transaction_connection_kills_do_not_hang: 3/3 pass.
  • test_transaction_abandoned_multi_is_discarded_before_refresh: 3/3 pass.
  • test_cluster_transaction.py: 16/16, run twice.
  • test_transaction_pipeline.py: 3/3.

I've updated the PR description to match.

🤖 Generated with Claude Code

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Split out as a follow-up: #580. txn_release_pin() pushes a lone key index into the old pin's m_key_index_pools, and cluster_client::create_request() later reads it as a command index (vector::_M_range_check). It reproduces with --transaction --pipeline 16 and CLIENT KILL on master, 2.5.2 and 2.4.0. This PR fixes the #569 wedge but not this.

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 --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection

1 participant