Skip to content

Preserve transaction lookahead when a cluster pin is released - #583

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/transaction-key-pool-580
Oct 13, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/transaction-key-pool-580

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Losing a cluster transaction pin before its lookahead key was consumed put a bare key index into a queue of (command_index, key_index) pairs. Pipelined reconnects could then interpret that key as a command index, throw vector::_M_range_check, or silently choose the wrong command for a small index.

Keep the unsent key in transaction lookahead state across pin release. When choosing a replacement pin, reconstruct the same key and resolve its owner using the current topology without advancing the iterator again.

Validation:

  • The new deterministic regression fails on master; a real three-primary Redis run also reproduces the worker exception.
  • All five unit programs pass, including ASan/LSan/UBSan builds.
  • The complete transaction suite passes over plaintext and TLS: 15 passing tests and two standalone-only skips in each.
  • Six sanitizer runs pass across both reported pipelined concurrency settings, with repeated shard connection kills and valid JSON results.
  • Formatting and the staged C++ format hook pass.

Fixes #580.


Note

Medium Risk
Changes cluster transaction pin release and re-pin key selection in cluster_client, which affects reconnect/MOVED paths; regression coverage is strong but the behavior is easy to get wrong.

Overview
Fixes #580: when a cluster --transaction rotation loses its pin before the lookahead key is sent, the old path pushed that key index into the per-connection routed queue. That queue is pairs of (command_index, key_index), so a lone index—especially a small one—could be treated as a command index and crash or mis-route under pipelined workloads and shard connection kills.

txn_release_pin() no longer returns the staged key to the pool; it keeps the unsent lookahead in m_txn_staged_key_index / m_txn_has_staged_key and only clears pin and round-key state. On re-pin, create_arbitrary_request reuses that staged index via generate_key instead of calling get_key_for_conn again, so the sequential iterator is not advanced twice.

Adds a deterministic cluster_transaction_test unit harness (wired into make check) and a cluster integration test that kills shard connections during pipelined MULTI/SET/EXEC with multiple clients.

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

@github-actions

Copy link
Copy Markdown

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

Thanks for this. I went through the pin-release and re-pin paths and it looks right to me. The old code pushed a bare key index into a queue of (command_index, key_index) pairs. Keeping the unsent key in m_txn_staged_key_index and rebuilding it with generate_key() when re-pinning fixes that at the source, and it doesn't advance the iterator a second time. Looking up the owner again against the current topology, instead of reusing the old pin's pool, also seems like the right call after a MOVED or disconnect.

It's also good to see a deterministic unit test that fails on master. It checks both the routed-pair queue staying intact (including the small key index that would quietly pass as a valid command index) and the sequential iterator not being advanced twice. That's stronger than relying only on the shard-kill integration test, which depends on timing.

Just checking one thing: as far as I can tell, the only place an unconsumed staged key still gets dropped is the reset when m_arbitrary_command_rotation_seq changes (and a full disconnect(), which the test covers). I think that's fine, because a mid-rotation pin loss re-pins at the same m_executed_command_index without wrapping. Can you confirm there's no reconnect path that wraps the rotation before the staged key is used?

This is cluster and transaction routing, so it may be worth a second look from whoever owns that code. Otherwise this looks good to me.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Reconnect and pin release do not themselves advance the command rotation (client.cpp advances only after a successful send). For the unsent lookahead covered by #580, re-pinning preserves the index and the pending keyed command consumes it before normal wrap.

There is a separate pre-existing edge case, so I would not claim that a staged key can never be dropped: losing the pin after the final keyed command can make circular lookahead stage a future key while sending a trailing keyless command such as EXEC. The subsequent rotation reset then drops that key. Both the circular lookahead and the wrap reset are unchanged from master. This PR fixes the routed-pair queue corruption and preserves the already-staged unsent key; it does not fix that existing tail case.

Seven independent high-effort reviewers inspected this candidate before opening the PR, including the transaction lifecycle and queue invariants. Human maintainer review remains for the next session.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Adversarial review summary: 7 independent reviewers each took one area (semantics, output compatibility, test discrimination, build/packaging, lifetime/state machine, interactions with #574–#576/#582/#578, maintainer standards). All 7 gave MERGE.

The bug is fixed:

  • Issue repro (--transaction --pipeline 16 / -t2 -c4 --pipeline 8 with CLIENT KILL loops): master hits _M_range_check or hangs in every run; this PR exits cleanly in every run, including under ASan+UBSan.
  • --requests 60000 -c2: the PR completes exactly 120000 ops. Master stops early, at 86397 and 21546.

Transaction behaviour is unchanged:

  • With no kills, every key routes to the correct slot and none are lost or duplicated. The --key-pattern=S ranges have no gaps, and memtier's counts match the server's commandstats.

Tests catch the bug:

  • The new unit test fails on master with FAIL: pin release must not append a bare key ... (#580).
  • The new RLTest fails on master in 3/3 to 6/6 runs per reviewer and passes on the PR.

Builds and packaging pass:

  • gcc and clang -Wall -Wextra.
  • ASan+UBSan make check.
  • i386 bookworm make check.
  • i386 dpkg-buildpackage (runs make check).
  • make distcheck.

Works alongside the other fixes: combined with #582 and the #578 fix, the cluster suites pass 46/46 on plaintext and on TLS.

Non-blocking follow-ups:

  1. Re-pin routing isn't checked. Mutating generate_key(staged_key_index) to generate_key(staged_key_index + 1) survives every test. A unit test with two connections and a populated slot map, asserting pin() == owner of the staged key after release, would close this.
  2. One assertion can never fail. In cluster_transaction_test.cpp:122, the "pin and old round key released" check passes regardless, because the round key was never set at that point. Adding check(!client.round_key_valid() && client.pin() == -1, ...) after line 147 kills the "no round-key reset" mutant.
  3. Commit message: the body is empty. Nit, from the maintainer-standards reviewer.
  4. Makefile.am conflict: there will be a small text conflict with Build and run natively on Windows (MSYS2 / MinGW-w64) #553 on check_PROGRAMS.

Pre-existing, not caused by this PR:

  • After shard connection kills, a MOVED/EXECABORT storm hits about 15–20% of transactions while the shard reconnects.
  • A pipelined release can split MULTI and SET across connections, causing EXEC without MULTI.
  • Coverage make check fails to link (__gcov_*): the vendored ax_code_coverage.m4 (serial 15) never defines CODE_COVERAGE_LIBS. Will be fixed separately.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit fa45424 into master Oct 13, 2026
122 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026


Mutation testing during the review of #582 and #583 found faults that the
merged tests did not catch. This change only touches tests.

test_cluster_redirect_stats.py (#582, #579):
- Check the Totals Time-Serie Count, Bytes RX and Bytes TX as well as the
  per-command series. Dropping m_total_cmd.update_op() in
  update_moved_set_op() used to pass.
- Delay every terminal reply from the scripted peer by 20 ms, and check
  Min/Average/p50 latency and each per-second Min Latency against that
  delay in every mode. A zero latency on a completed redirect (for example
  an ASK reply with retries disabled) used to pass the default-backoff
  cases.
- Report Ops/sec == 0 as an assertion instead of letting the redirect-rate
  division raise ZeroDivisionError and skip the remaining cases.
- Check the final errors/retries/retried_ops progress counters on the
  success, refused and transaction paths.

cluster_transaction_test.cpp (#583, #580):
- Add a two-shard test with a populated slot map. After txn_release_pin(),
  the next send must re-pin to the owner of the retained lookahead key.
  Generating the key after it used to pass.
- After releasing a pin whose round key was already consumed, check that the
  round key and the pin are both cleared. The existing "pin and old round
  key released" check runs before any round key is set, so it cannot fail.

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


Mutation testing during the review of #582 and #583 found faults that the
merged tests did not catch. This change only touches tests.

test_cluster_redirect_stats.py (#582, #579):
- Check the Totals Time-Serie Count, Bytes RX and Bytes TX as well as the
  per-command series. Dropping m_total_cmd.update_op() in
  update_moved_set_op() used to pass.
- Delay every terminal reply from the scripted peer by 20 ms, and check
  Min/Average/p50 latency and each per-second Min Latency against that
  delay in every mode. A zero latency on a completed redirect (for example
  an ASK reply with retries disabled) used to pass the default-backoff
  cases.
- Run the success mode with a 50 ms backoff too, and require each sample
  to cover both backoffs. Timing the reply from the last send instead of
  the first (m_sent_time in client::handle_response) used to pass.
- Check the final errors/retries/retried_ops progress counters on the
  success, refused and transaction paths. The stderr progress line is the
  only output that carries them.

cluster_transaction_test.cpp (#583, #580):
- Add a two-shard test with a populated slot map. After txn_release_pin(),
  the next send must re-pin to the owner of the retained lookahead key.
  Generating the key after it used to pass.
- After releasing a pin whose round key was already consumed, check that the
  round key and the pin are both cleared. The existing "pin and old round
  key released" check runs before any round key is set, so it cannot fail.

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


Mutation testing during the review of #582 and #583 found faults that the
merged tests did not catch. This change only touches tests.

test_cluster_redirect_stats.py (#582, #579):
- Check the Totals Time-Serie Count, Bytes RX and Bytes TX as well as the
  per-command series. Dropping m_total_cmd.update_op() in
  update_moved_set_op() used to pass.
- Delay every terminal reply from the scripted peer by 20 ms, and check
  Min/Average/p50 latency and each per-second Min Latency against that
  delay in every mode. A zero latency on a completed redirect (for example
  an ASK reply with retries disabled) used to pass the default-backoff
  cases.
- Run the success mode with a 50 ms backoff too, and require each sample
  to cover both backoffs. Timing the reply from the last send instead of
  the first (m_sent_time in client::handle_response) used to pass.
- Check the final errors/retries/retried_ops progress counters on the
  success, refused and transaction paths. The stderr progress line is the
  only output that carries them.

cluster_transaction_test.cpp (#583, #580):
- Add a two-shard test with a populated slot map. After txn_release_pin(),
  the next send must re-pin to the owner of the retained lookahead key.
  Generating the key after it used to pass.
- After releasing a pin whose round key was already consumed, check that the
  round key and the pin are both cleared. The existing "pin and old round
  key released" check runs before any round key is set, so it cannot fail.

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


Mutation testing during the review of #582 and #583 found faults that the
merged tests did not catch. This change only touches tests.

test_cluster_redirect_stats.py (#582, #579):
- Check the Totals Time-Serie Count, Bytes RX and Bytes TX as well as the
  per-command series. Dropping m_total_cmd.update_op() in
  update_moved_set_op() used to pass.
- Delay every terminal reply from the scripted peer by 20 ms, and check
  Min/Average/p50 latency and each per-second Min Latency against that
  delay in every mode. A zero latency on a completed redirect (for example
  an ASK reply with retries disabled) used to pass the default-backoff
  cases.
- Run the success mode with a 50 ms backoff too, and require each sample
  to cover both backoffs. Timing the reply from the last send instead of
  the first (m_sent_time in client::handle_response) used to pass.
- Check the final errors/retries/retried_ops progress counters on the
  success, refused and transaction paths. The stderr progress line is the
  only output that carries them.

cluster_transaction_test.cpp (#583, #580):
- Add a two-shard test with a populated slot map. After txn_release_pin(),
  the next send must re-pin to the owner of the retained lookahead key.
  Generating the key after it used to pass.
- After releasing a pin whose round key was already consumed, check that the
  round key and the pin are both cleared. The existing "pin and old round
  key released" check runs before any round key is set, so it cannot fail.

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 --pipeline > 1: lone key index left in m_key_index_pools crashes with vector::_M_range_check

1 participant