Skip to content

Don't continue a --transaction block after its MULTI is lost - #607

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/txn-multi-split-after-release
Oct 15, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/txn-multi-split-after-release

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --cluster-mode --transaction, if the connection holding a rotation's MULTI drops mid-block, memtier still sends the rest of the block. It goes either on the reconnected socket or on another shard's connection. The SET runs outside any transaction, and the EXEC fails with -ERR EXEC without MULTI. With -t 2 -c 4 --pipeline 8 and one shard's clients killed every 0.5s, that was 3–17 -ERR replies per run on master (#604).

Root cause

The pin decides which connection sends the next command of a rotation. Nothing checked that this is still the socket that holds the rotation's open MULTI. Instrumenting master showed two ways in, and every EXEC without MULTI matched one of them:

  1. The pin reconnects mid-block. shard_connection::disconnect() clears m_multi_open, and the server drops the MULTI with the socket. If no other connection released the pin meanwhile, the reconnected socket sends SET and EXEC.
  2. The pin moves mid-block. Another connection's hold_pipeline() sees the pin disconnected and calls txn_release_pin(). The lookahead then re-pins the tail of the block (SET, EXEC) on another connection.

The #583 review also described a variant where a MOVED on an earlier pipelined rotation releases the pin. On current master that release is gated by is_multi_open() (#569), and only the two disconnect paths above showed up.

Fix

  • m_txn_multi_conn_id records the connection that sent the rotation's MULTI opener. It is cleared when the EXEC/DISCARD is sent, when the rotation wraps, and on a full disconnect().
  • A later command of the block whose connection is not that one, or no longer has is_multi_open(), is skipped up to and including the EXEC/DISCARD. The rotation advances (advance_arbitrary_command_index()), nothing is sent, and nothing is counted. The server has already discarded what was queued, so memtier's per-command counts keep following the server's.
  • A skipped block may not have sent its lookahead key yet. The rotation-wrap reset no longer clears m_txn_has_staged_key, so that key goes to the next rotation and --command-key-pattern=S has no gap. A staged key can only be left at a wrap when its block was cut short.

Still needed after #606 and #615

Rebased onto master 38fae5d, which has #598, #606, #613 and #615.

  • Don't retry or replay --transaction requests (#611) #615 stops memtier from retrying or replaying --transaction requests. disconnect() now drops the in-flight requests. That covers commands that were already written when the socket died. It does not cover the commands of the block that were not generated yet. The rotation index keeps going, and the next fill_pipeline() generates the block's SET/EXEC on the reconnected socket or on a new pin. That is the path fixed here.
  • Hold a --transaction rotation while its key's shard reconnects #606 makes a rotation wait while its key's owner reconnects. That happens only when the rotation is unpinned. Mid-block, path 1 still applies: the owner reconnects, the rotation re-pins to it, and before this PR the SET/EXEC went out on the new socket without a MULTI. With this PR, m_txn_multi_conn_id is still that connection but is_multi_open() is false, so the tail is skipped.
  • Since Don't retry or replay --transaction requests (#611) #615 there is no replay, so !is_multi_open() on the MULTI's connection means the MULTI really is gone. Before Don't retry or replay --transaction requests (#611) #615, a replayed MULTI did not set m_multi_open (Bugbot's comment). That case no longer exists.

Conflicts were only adjacent lines: m_txn_wait_slot (#606) and m_txn_multi_conn_id (this PR) are both kept in the constructor, disconnect(), the rotation-wrap reset and the header. The tests are the union of both PRs.

Test evidence

New integration test test_transaction_lost_multi_is_not_continued: MULTI/SET/EXEC at -t 2 -c 4 --pipeline 8, with one non-main shard's clients killed every 0.5s for 8s. It asserts no EXEC without MULTI in stderr and errorstat_ERR == 0 on every primary.

runs result
origin/master e4d14e8 (before the rebase) 8 8/8 fail: 3–16 -ERR EXEC without MULTI per run
origin/master 38fae5d (after #606/#615), source fix reverted 1 fail: 11 -ERR EXEC without MULTI
this PR before the rebase 13 13/13 pass
this PR rebased on 38fae5d 1 pass

New unit check in tests/unit/cluster_transaction_test.cpp: drop the connection right after MULTI. Then SET, GET and EXEC must not be written to the output buffer, and the unsent staged key must be the one the next rotation sends. Rebased on 38fae5d:

  • With the source fix reverted, it fails ("command of a lost MULTI was not sent").
  • With the fix, it passes.
  • Before the rebase, a mutant with only the wrap reset restored failed at "next rotation sends the kept key".

On the rebased branch (3-shard cluster):

Out of scope: standalone --transaction

Standalone --transaction has the same split, and this PR does not change it. client::create_arbitrary_request() never checks is_multi_open(), so after a --reconnect-on-error reconnect the rest of the block goes out on the new socket. Test setup: standalone redis, MULTI/SET/EXEC, -t 2 -c 4 --pipeline 8 --reconnect-on-error, CLIENT KILL TYPE normal every 0.3s for 6s, no --retry-on-error. Result: 40 kills, 30 -ERR EXEC without MULTI, and memtier exits 0. That needs its own fix and test, so it is left as a follow-up.

Affected versions

2.4.0 through 2.5.2 (latest) and master. The pin, its release on disconnect, and the mid-rotation re-pin come from f7f8983 (#390), which is first in 2.4.0.

Fixes #604

Found during the review of #583. Related: #569, #576, #580, #583.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review. A human maintainer still needs to review this before merge.

Thanks for this. I went through the diff and the surrounding create_arbitrary_request() / fill_pipeline() paths. The fix looks right to me. A few notes:

  • I checked that m_multi_open is set when the MULTI is written (shard_connection.cpp, around line 2075), not when its +OK comes back. So a pipelined MULTI + SET on a healthy pin won't trip the new !is_multi_open() check and get skipped by mistake. Good.
  • The skip path returns false after advance_arbitrary_command_index(). shard_connection::fill_pipeline() keeps looping on that, so the whole tail through EXEC is dropped in one fill and the next rotation starts right away. I don't see a way for this to stall.
  • It also seems to cover a case the description doesn't list: the abandoned-MULTI DISCARD that send_conn_setup_commands() writes before a CLUSTER SLOTS refresh clears m_multi_open on the same socket. Any remaining commands of that block are now skipped as well, not sent without a MULTI.
  • You no longer clear m_txn_has_staged_key at the wrap, and I tried to break that. I think it's fine. The only other way I found to reach the wrap with a staged key is a re-pin lookahead that wrapped into the next rotation's keyed command. That key belongs to the next rotation anyway, so keeping it closes a small S-pattern gap rather than opening one.
  • Just checking: a skipped block is invisible except in benchmark_debug_log. Someone benchmarking under failovers will see fewer ops and Execs but no sign of how many blocks were abandoned. Would a once-per-rotation warning like m_txn_pin_lost_warned, or a counter, be worth it? Open question, not a blocker.

The integration test fails 8/8 on master and passes here, and the unit check fails with the source fix reverted. That's the fails-then-passes bar this repo has accepted before (PR#364). It's good to see standalone --transaction called out as a follow-up rather than folded in. Since this is cluster/transaction routing, it may be worth a second look from whoever owns that code, but I don't have anything blocking.

(I'd file this as a comment, not a formal block.)

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9c14747. Configure here.

Comment thread cluster_client.cpp Resolved
Nothing checked that the connection sending the rest of a rotation was
still the one holding the rotation's MULTI. When the pinned connection
dropped mid-block, the server discarded the MULTI with the socket, but
memtier still sent the rest of the block. Either the pin survived and the
reconnected socket sent it, or another connection released the pin in
hold_pipeline() and the lookahead re-pinned the tail elsewhere. The SET
then ran outside any transaction and the EXEC failed with "EXEC without
MULTI". With -t 2 -c 4 --pipeline 8 and one shard's clients killed every
0.5s, that was 4-17 -ERR replies per run.

Remember which connection carries the rotation's open MULTI. If a later
command of the block would go to another connection, or to one that no
longer has the MULTI open, skip it up to and including the EXEC/DISCARD
and advance the rotation without sending it. The server already
discarded what was queued, so memtier's counts keep matching the
server's.

A skipped block may not have sent its lookahead key yet. Keep that key
for the next rotation instead of clearing it when the rotation wraps, so
--key-pattern=S has no gap.

Fixes #604

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/txn-multi-split-after-release branch from 9c14747 to 20778b5 Compare October 14, 2026 11:49
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. I rebased onto 38fae5d (#606, #615 and the rest are in). Answers point by point:

  1. Skipping while the old owner still has its MULTI open. I agree the skip assumes the owner's MULTI is gone. After the rebase, the disconnect paths are covered: hold_pipeline() releases the pin only when the pin is conn_disconnected, and Hold a --transaction rotation while its key's shard reconnects #606's wait re-pins to the reconnected owner, which then fails is_multi_open(). Reading the code again, I found one path that can break the assumption today. The -MOVED/-ASK pin release (cluster_client.cpp, the Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569 guard) checks is_multi_open() on the connection that received the redirect, not on the rotation's MULTI owner. With --pipeline > 1, a redirect for an earlier rotation can arrive on connection X while the current rotation's MULTI is open on pin P. X has no MULTI open, so the pin is released while P's MULTI is still open. Before this PR, that sent P's SET/EXEC to another connection (EXEC without MULTI) and left P's MULTI open. With this PR, the tail is skipped and P's MULTI is still left open. So the PR doesn't make this worse, but it doesn't fix it either. This needs a slot migration during a pipelined run, and I have only traced it in the code, not reproduced it. The right fix is in the release guard (don't release while m_txn_multi_conn_id != -1), not in a log line, so I'll handle it as a follow-up with its own test.

  2. Keeping m_txn_has_staged_key across the wrap. The flag is cleared when a keyed command consumes the key, so only a key that was never sent can be carried over. Your cases:

    • WATCH __key__ / MULTI / SET / EXEC: the lookahead key is consumed by WATCH, before MULTI. A skip after MULTI leaves nothing staged.
    • A block skipped before its first keyed command: the key was generated once and never sent, so carrying it over fills what would otherwise be a gap. That is the case the unit check covers.
    • A mid-block re-pin whose lookahead wraps into the next rotation's keyed command: before this PR, the wrap reset dropped that key, which left a gap. Now the next rotation uses it.

    Hold a --transaction rotation while its key's shard reconnects #606's wait path stages a key with the pin at -1, but it returns before advancing the rotation, so it can't reach a wrap. I don't see a path that sends one key index twice.

  3. The skip path doesn't bump m_reqs_generated. That's intended. The server discarded those commands, so memtier doesn't count them. fill_pipeline() keeps looping, and with --requests the run just does a few more rotations to reach the target.

  4. The _reset_discard_calls() dependency. I added a comment on the helper saying that CONFIG RESETSTAT also clears the errorstats _error_count() reads. I didn't rename it: Hold a --transaction rotation while its key's shard reconnects #606's test and this one both depend on it, and a rename would touch unrelated tests that other open PRs also edit.

Also found while checking this: standalone --transaction has the same split after a --reconnect-on-error reconnect. I got 30 EXEC without MULTI in 6s with 40 client kills. Details are in the PR body under "Out of scope". It is left as a follow-up.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 0c4c2f7 into master Oct 15, 2026
127 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 15, 2026
…ps its MULTI

With --transaction against a single endpoint, a connection killed after
it sent a rotation's MULTI reconnects with the rotation index still on
the next command of the block. The rest of the block (SET, EXEC) then
went out on the new socket: the SET ran outside any transaction and the
EXEC failed with "EXEC without MULTI", while the run still exited 0.

client::create_request() (the standalone path) now remembers that this
rotation's MULTI was sent. If the connection no longer has the MULTI
open, it skips the rest of the block up to and including the
EXEC/DISCARD, without sending or counting it. The server already
discarded what was queued. This is the same rule #607 adds for cluster
mode in cluster_client::create_arbitrary_request().

Fixes #633

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 15, 2026
…ps its MULTI

With --transaction against a single endpoint, a connection killed after
it sent a rotation's MULTI reconnects with the rotation index still on
the next command of the block. The rest of the block (SET, EXEC) then
went out on the new socket: the SET ran outside any transaction and the
EXEC failed with "EXEC without MULTI", while the run still exited 0.

client::create_request() (the standalone path) now remembers that this
rotation's MULTI was sent. If the connection no longer has the MULTI
open, it skips the rest of the block up to and including the
EXEC/DISCARD, without sending or counting it. The server already
discarded what was queued. This is the same rule #607 adds for cluster
mode in cluster_client::create_arbitrary_request().

Fixes #633

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…ps its MULTI

With --transaction against a single endpoint, a connection killed after
it sent a rotation's MULTI reconnects with the rotation index still on
the next command of the block. The rest of the block (SET, EXEC) then
went out on the new socket: the SET ran outside any transaction and the
EXEC failed with "EXEC without MULTI", while the run still exited 0.

client::create_request() (the standalone path) now remembers that this
rotation's MULTI was sent. If the connection no longer has the MULTI
open, it skips the rest of the block up to and including the
EXEC/DISCARD, without sending or counting it. The server already
discarded what was queued. This is the same rule #607 adds for cluster
mode in cluster_client::create_arbitrary_request().

Fixes #633

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
#634)

With --transaction against a single endpoint and --reconnect-on-error,
a connection killed after sending a rotation's MULTI resumed the block
on the new socket. The SET ran outside any transaction and EXEC failed
with "EXEC without MULTI", while the run still exited 0.

client::create_request() now tracks whether the rotation's MULTI was
sent (m_txn_multi_sent). Outside cluster mode, if the connection no
longer has the MULTI open, the rest of the block through its EXEC/DISCARD
is skipped and not sent. The next rotation starts with a fresh MULTI, and
commands after the EXEC are sent as normal. Cluster mode keeps its own
check in cluster_client::create_arbitrary_request() (#607).

Docs: --help and the man page's --transaction entry (regenerated from
usage()) now say a reconnect inside a MULTI block skips the rest of it
uncounted, that replies already received stay counted, and that the
same-key behavior applies in every mode.

Adds test_retry_on_error.py::test_transaction_lost_multi_is_not_continued.

Fixes #633
Refs #673

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 --transaction: rest of a MULTI block is sent after its MULTI was lost (EXEC without MULTI)

1 participant