Skip to content

Resend requests after the setup ladder on a new connection - #613

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix/replay-after-setup-ladder
Oct 14, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix/replay-after-setup-ladder

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --retry-on-error --reconnect-on-error, the requests in flight on a dropped connection are resent once it reconnects. They were resent before the connection's setup commands (AUTH, SELECT, HELLO, READONLY, CLUSTER SLOTS). Redis runs a connection's commands in order, so:

  • with --authenticate, every resent request is answered -NOAUTH and counted as an error;
  • with --select-db, the resent requests run against database 0;
  • in cluster mode, they run ahead of READONLY and CLUSTER SLOTS.

Root cause

handle_event(BEV_EVENT_CONNECTED) called drain_replay_queue_after_reconnect() before fill_pipeline(), and fill_pipeline() is the only place that sends the setup ladder.

Fix

The drain in handle_event() is removed. The resend now happens in fill_pipeline(), which already had a drain gated on is_conn_setup_done() for requests handed over from a retired connection (#575):

  • When requests are waiting to be resent and the ladder isn't done, fill_pipeline() sends the ladder and returns.
  • When the ladder's replies come in, the existing guarded drain resends the waiting requests, still ahead of fresh work.
  • The ladder is sent even if the run's stop condition has already been reached, because the resends still need their replies. Without this, a finished run could leave them stranded.
  • It keeps the existing hold on CLUSTER SLOTS while a pinned --transaction MULTI is open (Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569).

What this leaves alone:

Second commit: ending the stats at a --test-time deadline

After the first commit, a reconnect that lands after the deadline:

  1. sends the setup ladder;
  2. gets the SELECT/AUTH reply, which reaches process_response() with finished() already true, so it calls set_end_time();
  3. only then resends the in-flight requests.

run_stats::set_end_time() reports the current second's bucket without rolling it. The resends' replies then went into a bucket that was never reported, and the next roll reported the end bucket a second time.

Effect, with a proxy that stops replies at 1.8 s of --test-time 2 and closes at 2.1 s (-c 2 --pipeline 32, with -a pw or --select-db=3):

  • JSON Count is 64 short of the last progress line.
  • Connection Errors is 4 instead of 2.

process_response() now ends the stats only when finished() && all_connections_idle(), the same condition as the two other end-of-run checks (the fill_pipeline() "done" branch and the rate-limit timer). Measured with the same proxy:

Count vs last progress line Connection Errors
first commit only, 6 runs (-a / --select-db) 64 short every time 4
both commits, 6 runs equal 2

The server's SET calls are Count + 64, from the 64 requests that ran once and were resent after their replies were lost.

Behaviour change (release notes)

  • --test-time totals now include the replies in flight at the deadline. Before, a client's stats ended at the first reply read after the deadline. Replies to requests still in flight, read later, were left out of the report. With deep pipelines that is noticeable: -c 4 -t 2 --pipeline 256 -d 4000 --test-time 3 reported a Count 2008 short of the server's cmdstat_set calls on master, in 2/2 runs. Now the stats end when every connection is idle. Count equals the server's count (0 short in 2/2 runs), and the duration runs to the last reply: 3010 ms instead of 3000 ms in that run. If a shard is slow to answer after the deadline, the duration stretches by that much too, and ops/sec reflects it. Before, those replies were not reported at all. A shard that never answers leaves the run waiting for that reply either way; that part is unchanged.
  • With --authenticate, resends are no longer counted as rejected operations. On master, every request resent after a reconnect was answered -NOAUTH, and those replies were counted in Count and Ops/sec (and as errors). Now the resends run after AUTH and are counted as normal operations.

Third commit: review follow-ups

  • test_replays_after_deadline_are_counted now also counts, at the proxy, the SETs sent on the connections opened after the stall, and expects clients * pipeline (64). The mutant that skips resends once the run is finished (!m_conns_manager->finished() && added to the resend block in fill_pipeline()) passed the old assertions. It now fails 5/5 with 0 == 64 "requests in flight were not resent"; this PR passes 5/5.
  • The --transaction hold on CLUSTER SLOTS, which was checked twice in fill_pipeline(), is now setup_held_for_transaction().
  • A leftover comment in handle_event() is gone. The fill_pipeline() comment now says which held requests are not resent when the run ends: those on a peer that is not connected (Count retries still queued when a --test-time run ends (#619) #620 accounts for those). It also says when the transaction hold lets fresh work go ahead of resends.

Interaction with #620 (#619): #620 changes client::disconnect_all(), which runs after set_end_time() in the fill_pipeline() "done" branch. This PR doesn't touch that path or its order. The two only conflict textually in tests/test_retry_on_error.py: both edit the imports and append tests at the end. I merged both locally. With the merged binary and merged tests, test_replays_after_deadline_are_counted, #620's test_retries_left_at_end_of_test_time_are_counted and test_replays_wait_for_auth_after_reconnect each passed 2/2. Merge order: #613, then #620, then #600. #620 and #600 will each need a trivial rebase of the test file.

Tests

There are four new tests. Two are in tests/test_retry_on_error.py, both standalone only. Each runs --pipeline=32 -c 2 for 6 s and sends 4 waves of CLIENT KILL TYPE normal:

  • test_replays_wait_for_auth_after_reconnect sets requirepass for the run and asserts that no NOAUTH reply appears.
  • test_replays_wait_for_select_after_reconnect uses --select-db=3 and asserts that DB 3 has keys and DB 0 has none.

The other two:

  • test_replays_after_deadline_are_counted (standalone, plain TCP) puts the stalling proxy above in front of the server, with --select-db=3. It asserts that JSON Count equals the last progress line and that Connection Errors == 2.
  • test_replica_replays_wait_for_readonly, in tests/test_read_preference_failover.py (OSS cluster with replicas), runs --read-preference=secondary --ratio=0:1 --pipeline=32. It sends CLIENT KILL TYPE normal waves on the replicas and asserts that the replicas' errorstat_MOVED is 0.

Results:

master e4d14e8 this PR
auth test, 8 runs 8/8 FAIL (256 NOAUTH each) 8/8 PASS
select test, 8 runs 8/8 FAIL (158-256 keys in DB 0) 8/8 PASS
both tests under --tls FAIL (256 NOAUTH / 241 keys) PASS
replica READONLY test (cluster + replicas) 2/2 FAIL (672, 730 MOVED on replicas) 2/2 PASS
deadline accounting test 8/8 FAIL on the first commit alone (Count 64 short, Connection Errors 4 vs 2); passes on master, where the resends went out ahead of SELECT 8/8 PASS

With all three commits rebased on 013cb85 (which includes #591, #593, #599, #612 and #615), these all pass:

  • Standalone test_retry_on_error.py: 15 passed.
  • 3-shard OSS cluster test_retry_on_error.py: 6 passed, 9 skipped (standalone-only).
  • 3-shard OSS cluster with replicas (--use-slaves) test_read_preference_failover.py: 2 passed, 1 skipped (the existing placeholder).

Earlier, with the first two commits on a7b9fc9 (which includes #591), these suites all passed:

  • Standalone: test_retry_on_error.py, test_reconnections.py, tests_oss_simple_flow.py, test_json_output_integrity.py.
  • 3-shard OSS cluster: test_retry_on_error.py, test_reconnections.py, test_cluster_redirect_stats.py.
  • 3-shard OSS cluster with replicas: test_read_preference_failover.py, test_read_preference_modes.py, test_cluster_failover_retry.py.

The first commit alone also passed test_cluster_transaction.py on the 3-shard cluster.

Manual repro: -a pw -c 2 --pipeline 32 with 40 CLIENT KILLs. Master gives 256 NOAUTH in each of 3 runs; this PR gives 0.

Affected versions

Introduced by 08cfc24 (#381, --retry-on-error), so the first affected tag is 2.4.0. Affected: 2.4.0 through 2.4.4, 2.5.0, 2.5.1, 2.5.2 and master. Confirmed by running the repro on 2.4.0, 2.5.2 and master; the versions in between are derived from history. 2.3.x and older have no --retry-on-error.

Fixes #527

🤖 Generated with Claude Code


Note

Medium Risk
Changes reconnect/replay ordering and end-of-run stats timing in the hot fill_pipeline/process_response path; behavior is well-covered by new integration tests but affects retry-on-error and test-time accounting.

Overview
Fixes #527: with --retry-on-error and reconnects, in-flight requests are no longer resent immediately on BEV_EVENT_CONNECTED. Replay draining moves into fill_pipeline(), which sends the connection setup ladder (AUTH, SELECT, HELLO, READONLY, CLUSTER SLOTS) first and only then drains the replay queue—still ahead of new work, including after --test-time has elapsed.

Adds setup_held_for_transaction() so CLUSTER SLOTS stays deferred while a pinned --transaction MULTI is open (same behavior as before, refactored).

Stats at deadline: process_response() now calls set_end_time() only when the run is finished() and all_connections_idle(), so late replies from post-deadline reconnect resends are counted once and connection-error totals stay correct.

Tests: churn/kill scenarios for AUTH, SELECT, replica READONLY ordering, and a stalling-proxy case for deadline accounting.

Reviewed by Cursor Bugbot for commit 24dfedf. 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. I went through the diff and the description. Moving the resend into fill_pipeline() behind the existing is_conn_setup_done() gate seems like the right fix to me, and it's a smaller change than adding a second ordering rule in handle_event(). Each of the four tests covers one of the broken scenarios and, going by the table, fails on master or on the first commit alone. A few questions:

  1. The process_response() change to finished() && all_connections_idle() affects more than the reconnect case, I think. Before, a normal --test-time run with no disconnects ended its stats on the first reply read after the deadline. Now it ends them on the last reply, once every pipeline has drained. With a deep --pipeline against a slow server, that could make Duration a bit longer and change ops/sec in the JSON and the summary for runs where no reconnect happened. The fill_pipeline() "done" branch already used this condition, so maybe the effect is negligible in practice. Did you check whether Duration or ops/sec move on a plain run (no kills, --pipeline 32 or more)? test_json_output_integrity.py passing shows the totals still agree with each other, but I'm not sure it would catch a change in Duration.

  2. Just checking: the old drain in handle_event() was gated on m_config->retry_on_error, and the new path in fill_pipeline() isn't. I guess m_replay_queue can only be non-empty when retries are on, or when adopt_replay_request handed requests over (Resend held retries when a slot map refresh retires a connection #575), so this is equivalent. Do you confirm?

  3. In _StallingProxy._pump, late_sets counts $3\r\nSET\r\n in each recv() chunk, and the test then checks late_sets == clients * pipeline exactly. If a SET header is split across two reads, it isn't counted and the test fails for no real reason. That's unlikely with 32 small commands, but keeping the tail of the previous chunk, or counting over the whole byte stream for each connection, would remove the risk.

Nitpicking: setup_held_for_transaction() only reads state, so it could be const, like peer_client_has_any_setup_in_progress() next to it.

The ordering fix looks solid to me. I'd mainly like an answer on (1) before merge, since it may change Duration and ops/sec for every --test-time run, not just runs that hit #527. That may be worth a second look from whoever owns run_stats.

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

fcostaoliveira and others added 3 commits October 14, 2026 07:58
With --retry-on-error, the requests in flight on a dropped connection
are resent once it reconnects. handle_event() resent them as soon as
the connection came up, before fill_pipeline() sent the setup commands
(AUTH, SELECT, HELLO, READONLY, CLUSTER SLOTS). The server runs a
connection's commands in order, so with --authenticate every resent
request was answered -NOAUTH and counted as an error, and with
--select-db it ran against database 0.

fill_pipeline() already resends requests handed over from a retired
connection only once the setup ladder is done. Let it handle the
reconnect case too: when requests are waiting to be resent and the
ladder is not done, send the ladder and return; the resends follow when
its replies come in, still ahead of fresh work. This is done even after
the run's stop condition is reached, as the resends are still owed a
reply.

Fixes #527

Co-Authored-By: Claude Opus 5.5 <[email protected]>
process_response() ended the run's stats as soon as the run was finished.
Since the previous commit, a reconnect that lands after a --test-time
deadline sends the setup ladder first and the resends only once its
replies are in, so the stats ended on the SELECT or AUTH reply, with the
resends not even sent. run_stats::set_end_time() reports the current
second's bucket without rolling it, so the resends' replies went into a
bucket that was never reported, and the next roll reported the end
bucket a second time: Count fell short of the last progress line by the
resent requests, and Connection Errors doubled.

End the stats there only once every connection is idle, like the two
other end-of-run checks do.

Also test the cluster side of #527: reads resent to a replica before
READONLY were answered -MOVED.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The deadline test only compared Count with the last progress line, which
still held if the requests in flight were never resent once the run was
finished. Count the SETs sent on the connections opened after the stall
and expect every request that was in flight.

fill_pipeline() checked the --transaction hold on CLUSTER SLOTS twice;
give it a name. Drop a leftover comment in handle_event() and say which
held requests are not resent at the end of a run, and when the
transaction hold lets fresh work go ahead of resends.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/replay-after-setup-ladder branch from 2e6ebd0 to 24dfedf Compare October 14, 2026 07:04
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Point by point:

  1. End time outside the reconnect case. Yes, that's intended, and it's now in the PR description under "Behaviour change (release notes)". The old end time lost the replies that were in flight at the deadline. With -c 4 -t 2 --pipeline 256 -d 4000 --test-time 3, master reported a Count 2008 short of the server's cmdstat_set calls, in 2/2 runs. With this PR the gap is 0 in 2/2 runs, and the duration went from 3000 ms to 3010 ms. Limiting the change to runs with outstanding resends would keep that under-count in every deep-pipeline timed run. The commit message for 2fa9563 only covers the reconnect case; the PR description now covers both.

  2. A connection that is slow to go idle after the deadline. If a shard answers late, the end time is now taken at its last reply, so the duration includes the wait and ops/sec reflects it. Before, those replies were left out of the report. A shard that never answers doesn't let the run finish either way: fill_pipeline()'s "done" branch has always needed every connection idle before disconnect_all(). That case ends through an interrupt or the worker-failure path, and both call finalize_all_clients() to stamp the end time, as before. So the duration only goes past --test-time by the time the replies actually took to arrive.

  3. Fall-through under the pinned-MULTI hold. Agreed, that's the existing Resend held retries when a slot map refresh retires a connection #575 behaviour. It can't happen after a reconnect, because connect() and disconnect() both clear m_multi_open. Only requests handed over to a live connection can meet it. There's now a comment saying so, and the hold is factored into setup_held_for_transaction(), which both checks use (24dfedf).

  4. Timing in test_replays_after_deadline_are_counted.

    • The assertions don't depend on where the deadline lands relative to the 1.8 s / 2.1 s stall. The proxy's clock starts at memtier's first connection, and memtier reconnects after its default 1 s delay, at about 3.1 s, which is after the deadline unless memtier's clock lags the proxy's by more than a second.
    • Each of the clients connections opened before the stall is closed exactly once, so the number of reconnections is exact by construction.
    • The test now also checks, at the proxy, that the clients * pipeline requests in flight are resent on the new connections. A mutant that skips resends once the run is finished passed the old checks and now fails 5/5. The fix passes 5/5 here, plus 8/8 before that.
    • I haven't run it under the sanitizer builds locally; CI on this push will show that.
  5. Description. Updated. It now lists all four tests and has the stats-ending change, the behaviour notes, and the Count retries still queued when a --test-time run ends (#619) #620/Send ASKING before resending a request after -ASK #600 merge order.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 18f8266 into master Oct 14, 2026
127 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
After the replay block was rewritten (#613), say directly which retries
the fill_pipeline() drain is for. Pass --authenticate when the test
cluster has a password, so the reconnect case also runs with AUTH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
After the replay block was rewritten (#613), say directly which retries
the fill_pipeline() drain is for. Pass --authenticate when the test
cluster has a password, so the reconnect case also runs with AUTH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
With --retry-on-error, the requests in flight when the connection drops
are held for replay until the setup ladder succeeds (#613). After a
rejected AUTH that never happens, but the deadline check in
handle_reconnect_timer_event() required an empty replay queue, so the run
still reconnected forever (#625).

Give up once the run is over regardless of held replays. If no reply is
pending, disconnect_all() ends the client and counts the held requests as
run-ended-before-resend errors.

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

* Reconnect when a setup step fails on a reconnect

With --reconnect-on-error, if the server's password changes mid-run and the
connections are then dropped, memtier reconnects, logs "Reconnection
successful", gets -WRONGPASS for AUTH, and hangs until killed. --test-time,
--requests and --max-reconnect-attempts are all ignored.

An AUTH, SELECT or HELLO error reply only set error and returned from
process_response(). The connection stayed open with its setup unfinished,
so no workload request was ever sent on it again, and the --test-time and
--requests checks, which run when requests are sent or answered, were never
reached. --connection-stage-timeout does not help either: it stops checking
once the run has reached steady state. A wrong password at startup only
ends because of it.

With --reconnect-on-error, handle a setup error like a dropped connection,
as READONLY errors already are: attempt_reconnect() disconnects and
schedules the next attempt, so --max-reconnect-attempts bounds it.
Without the option, startup is unchanged.

With unlimited attempts, the reconnect timer now leaves the connection
disconnected once the run is finished (and nothing is held for replay), so
the thread ends at --test-time and the run fails as incomplete instead of
reconnecting forever.

"Reconnection successful" was logged when the TCP connect was started,
before the setup ladder ran. Log it on the first successful response after
the setup ladder, where the attempt counter is already reset.

Fixes #625

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

* Stop reconnecting at the deadline only after a rejected setup step

The previous commit left any connection disconnected when its reconnect
timer fired after the run's deadline. A plain connection drop near the end
of a --test-time run then left that client without an end time, and the run
failed as incomplete (test_json_aggregated_connection_errors_sec and
test_F17_monotonicity_under_churn in the sanitizer jobs). Before, the
reconnect went through and the client ended on its first response.

Only stop when the pending reconnect follows an AUTH/SELECT/HELLO rejection,
which is the case that otherwise retries forever with unlimited attempts.
The run then fails as incomplete, with a message saying why.

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

* Set the test password after the TLS setup connections are opened

Under TLS, add_required_env_arguments() opens new connections to the
server to set tls-protocols. The AUTH tests set requirepass before it, so
those connections failed with "Authentication required", outside the
try/finally that clears the password, and every later test on the server
failed the same way.

Set requirepass right before memtier starts, inside the try. Clear it
through a connection authenticated with the new password if the pool has
to open one.

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

* Stop reconnecting after a rejected setup even with replays held

With --retry-on-error, the requests in flight when the connection drops
are held for replay until the setup ladder succeeds (#613). After a
rejected AUTH that never happens, but the deadline check in
handle_reconnect_timer_event() required an empty replay queue, so the run
still reconnected forever (#625).

Give up once the run is over regardless of held replays. If no reply is
pending, disconnect_all() ends the client and counts the held requests as
run-ended-before-resend errors.

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

* Check that held replays are counted when a rejected setup ends the run

Without this, the test passed even if the timer path dropped the requests
held for replay instead of handing them to disconnect_all().

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

---------

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.

Replayed requests can race ahead of the connection-setup ladder (AUTH/SELECT/HELLO/READONLY) after a reconnect

1 participant