Skip to content

End a --test-time run at its deadline while reconnecting to a dead server - #666

Merged
fcostaoliveira merged 5 commits into
masterfrom
fix-656-reconnect-deadline
Oct 17, 2026
Merged

fcostaoliveira merged 5 commits into
masterfrom
fix-656-reconnect-deadline

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Decisions (issue #656 asked "is this intended?"): with --reconnect-on-error:

  • --test-time, unlimited attempts (the default): the deadline wins. Once the run has started and the deadline has passed, a failed connect is not retried. The client ends as it does when the deadline passes with all connections up: the report is printed, the exit code is 0, and the connection errors are in the report.
  • --test-time, finite --max-reconnect-attempts: unchanged from master. The deadline logic in this PR (capped delays, stopping at the deadline) applies only with unlimited attempts. A bounded run keeps its backoff schedule; it fails (exit 1) when its attempts run out, and ends normally when the server comes back in time, around the deadline included. This is the compatible option asked for in review: an earlier round failed bounded runs at the deadline, which also failed runs that recover (a node failing over or restarting around the deadline). For a started client, run_is_over() adds its wall-clock check only when stops_at_deadline() holds. Never-started clients retain master's benchmark-deadline rule (Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667).
  • A setup rejected since the connection's last workload reply still fails the run (Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630), even if the server then goes away and the last failure is a failed connect. Any workload reply, error replies included, shows the connection serves requests again and clears it. Only AUTH, SELECT and HELLO rejections count: an empty or error CLUSTER SLOTS reply (Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667, e.g. a node with no slots yet during a failover) is retried the same way but does not set the flag, so a node that then goes away ends the run normally. A rejected CLUSTER SLOTS refresh whose retry falls due at the deadline also gets one more refresh after the run is over (only for a client that has started), so a one-off slot error in the last second ends the run normally, as on master; one that persists still fails it (Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667).
  • --requests, unlimited attempts: the run keeps waiting for the server, which is what "unlimited" means, and this is now documented. memtier warns once, after 10 failed attempts, that the server is still unreachable.
  • Connection Errors count more failures under a unix socket. A unix-socket reconnect that fails inside reconnect() never reaches the event loop, so it was not counted. It is now counted (1 drop + N failed connects), the same as TCP. Tests pin it for both transports.

Release notes

  • TLS chaos runs: a client that never completes a request can now make a run fail with exit 1 when repeated handshake/connection kills prevent valid work; master could return success while overshooting the deadline. This is intentional, alongside the documented unix-socket Connection Errors change.
  • With --reconnect-on-error and unlimited --max-reconnect-attempts (the default), a --test-time run that has started ends at its deadline (exit 0, with its report) while its server is down, instead of reconnecting forever.
  • With a finite --max-reconnect-attempts nothing changes: the attempts bound the run at their usual backoff, and running out of them fails it (exit 1).
  • Unix-socket runs report more Connection Errors: a reconnect that fails before reaching the event loop is now counted (1 drop + N failed connects, as over TCP).

Problem

With --reconnect-on-error and the default --max-reconnect-attempts 0, a server that goes away for good kept a --test-time 5 run reconnecting once a second, forever, with no report. On master 0298524 it was at 380% when I killed it after 19 s.

A later review found a related overshoot. The deadline was only checked when a reconnect failed. A reconnect armed just before the deadline still waited out its full backoff delay (capped at MEMTIER_BACKOFF_CAP_SEC, 60 s). The run ended at that failed attempt, and the report counted the dead time. For example, --test-time 4 --reconnect-backoff-factor 20 with the server killed at 1.5 s exited after 22 s and reported Total duration 21831 ms and 20 ops/sec instead of ~114.

Root cause

  • attempt_reconnect() and the retry branch of handle_reconnect_timer_event() reschedule the reconnect whenever attempts are unlimited. They never check whether the run is over, and they arm the timer for the full backoff delay.
  • finished() (--test-time, --requests) is otherwise checked only when requests are sent or answered. That never happens again on a connection that cannot connect.

Fix (all in the reconnect path)

  • run_deadline() / run_is_over() compute the stop condition in one place.
  • next_reconnect_delay() (unlimited attempts only) caps the delay the reconnect timer is armed with at the time left before the deadline (within 1 ms counts as passed, so a timer firing a hair early by the wall clock does not re-arm near-zero delays). The cap applies to a local copy, so m_current_backoff_delay and the backoff schedule are unchanged.
  • stop_reconnecting_if_run_over() runs when a connect fails: in attempt_reconnect(), in the synchronous reconnect() < 0 branch, and before reconnecting in the timer. If the run is over, it disconnects the connection. If no replies are pending, it ends a started client's stats at the deadline, not at the failed attempt, so Total duration and ops/sec reflect --test-time, and calls disconnect_all(). That counts any requests held for replay as errors.
  • A failed connect stops the reconnects; a drop gets one reconnect. m_connect_failed records whether the error arrived before the connection was established, or reconnect() failed. It is cleared when a connection is established.
    • After the run is over, the first drop of an established connection is still reconnected, so a live server that closes connections around the deadline (e.g. a CLIENT KILL loop) still gets the held requests resent.
    • A second drop after the run is over (m_dropped_after_run_over) counts as a failed connect and ends the run. Without it, a peer that accepts and then closes every connection (a proxy with a dead backend, a TLS terminator, maxclients) with --retry-on-error was reconnected with zero delay forever, at 100% CPU and with no report.
    • Because of that one reconnect, a peer that closes connections after a delay can extend the run by up to about twice that delay past --test-time (in the README).
    • This replaces the previous m_reconnect_attempts == 0 "first reconnect" rule, which was unreliable because the counter is reset only on a reply.
  • A client that never got a reply stops at the deadline but did no work, so the run fails with exit 1 ("client(s) stopped before reaching their stop condition"), as for any unended client. It is not given the CLUSTER SLOTS refresh after the deadline (Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667: it fails the run). If its connection is dropped and its first connection only completes after the deadline (e.g. a server killing connections before their first reply), it is timed from the benchmark's start (process_first_request()), so it ends at its first reply instead of running a whole --test-time after the deadline, and the run ends normally.
  • Siblings are not torn down. Both end-of-run paths (a failed connect, a rejected setup) go through end_client_after_run(), which ends the client only if no other connection (e.g. another cluster shard) has a reply pending, a reconnect pending, or requests held for replay on a live connection (siblings_done()). A failed run is recorded on the client (client::fail_run()), so a busy sibling's later normal end still fails it. Pinned by tests/unit/reconnect_siblings_test.cpp: a sibling with a reconnect pending, and a connecting sibling holding requests for replay, each keep the client going (fails if the guard is reverted to all_connections_idle() or dropped, or if the replay check is removed).
  • Cluster scope: after the deadline only the main connection's held replays are resent by its own reconnect. Another shard's connection is reconnected through a slot map refresh, which still redials it after the deadline; if that dial fails, the connection is left closed and what it held counts as errors. A sibling's reconnect pending at the deadline can revive a stopped main connection once (it is the only way a shard reconnects); that is bounded and ends the client either way.
  • count_connection_error(): every dropped connection and failed connect, including the synchronous reconnect() < 0 one, counts as a connection error in one place.
  • Cluster: with unlimited attempts, when a dial in a CLUSTER SLOTS refresh fails after the run is over, the failure is counted as a connection error and the connection is left dead. Ending the client from there would disconnect the main connection in the middle of handling its reply.
  • Unneeded replicas (End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750 interplay): a replica connection whose setup is rejected under --read-preference primary is closed and left closed with --reconnect-on-error too, as End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750 does without it. With --reconnect-on-error, secondaryPreferred now applies the same rule because its reads fall back to the primary. Without reconnect, secondaryPreferred retains End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750's behavior. A secondary replica remains required: an unrecovered setup rejection fails the run, even if a sibling ends the client first. Redialed instead, it was rejected about once a second, and its retry, due at the deadline, then failed a run the primaries served in full (rc 1 on a 3+3 cluster with requirepass only on the primaries; master rc 0).
  • warn_reconnecting_indefinitely(): with unlimited attempts and --requests, memtier warns once per process (also across --run-count runs), after the first connection reaches 10 failed attempts: warning: server still unreachable after N reconnection attempts; ....

A server that is dead from the start is not this PR's deadline case. --test-time is measured from the start of the run, which never starts. That startup phase is bounded by the --connection-stage-timeout supervisor (#426): exit 2 once it expires (30 s by default). This PR leaves that unchanged and adds a test that pins it.

Interaction with #669 (10 s drain grace, now on master): the two compose. #669 ends a client that still has replies pending 10 s after the deadline; this PR ends a client whose connection is down, at the deadline. test_test_time_reconnecting_client_left_alone used to expect a 15 s backoff to run past the grace period; the reconnect is now due at the deadline, so it asserts the run ends before the grace period. That test no longer reaches the grace window, so #669's idle-client guard (if (all_connections_idle()) return 0; in abandon_unanswered_requests()) is now covered by test_reconnect_hanging_past_drain_grace_is_left_to_connection_timeout. An accept-and-hold peer (accepts, never replies) with --retry-on-error ends through #669's grace: exit 0 at about --test-time + 10 s.

Relationship to #657 (a server that stops replying after the deadline, fixed separately in the drain code): both are runs that never end. In #657 the connection stays up with replies pending; here the connection cannot connect.

Test evidence

New tests in tests/test_reconnections.py. They run memtier through an in-test proxy (TCP, or a unix socket under --unix) and stop it mid-run. The tests are skipped on cluster envs.

test what it pins before (f189250 / c6e43b0) this branch
test_server_gone_unlimited_reconnects_honor_test_time exit 0 within --test-time + 5 s, the report, no --requests warning after more than 10 attempts (fails if it warns without --requests, M11), Connection Errors ≥ 4 (failed connects counted) pass pass
test_server_gone_long_backoff_ends_at_test_time --reconnect-backoff-factor=20: wall time < --test-time + 3 s, JSON Total duration within [−100, +500] ms of --test-time FAIL (20.5 s) pass (4000 ms)
test_server_gone_never_started_client_ends_at_test_time -t2 -c1 and -t1 -c2, proxy forwards only the first connection: the never-started client stops too, exit 1 FAIL (killed at 21 s, both shapes) pass
test_accept_then_close_peer_with_retry_on_error_ends_at_test_time peer accepts then closes every connection after 2 s, --retry-on-error: exit 0 within --test-time + 4 s, with the report FAIL at c6e43b0 (killed at 21 s) pass (exit 0 at 5.0 s)
test_reconnect_hanging_past_drain_grace_is_left_to_connection_timeout the port stops answering SYNs (full accept queue); the reconnect armed for the deadline hangs until --connection-timeout=12, past the 10 s grace: no "got no reply" stall, the timed-out connect ends the run with exit 0, Total duration ≈ --test-time. Fails with #669's idle-client guard removed (G1) new pass 4/4 (15.0 s)
test_live_server_drop_after_deadline_resends_held_requests proxy holds replies from just before the deadline and drops the connections just after it, --retry-on-error: reconnected, no "run is over", no abandoned replays, exit 0. Fails with the !m_connect_failed gate removed (M5) or the flag forced true (M5b) new pass 5/5
test_cluster_slots_bad_reply.py::test_cluster_slots_empty_reply_then_node_gone_ends_at_test_time a refresh gets an empty CLUSTER SLOTS reply, then the node goes away: exit 0, "run is over", no "setup failed earlier" FAIL at a4b758c (exit 1, "setup failed earlier") pass
test_cluster_slots_bad_reply.py::test_cluster_slots_one_empty_reply_at_deadline_is_retried one empty CLUSTER SLOTS reply in the last second (moved_after 2300 and 2700, --test-time=3 --rate-limiting=1000): exit 0, no "Connection setup failed and the run is over" FAIL at b40c532 (exit 1, 2/2) pass
test_cluster_slots_bad_reply.py::test_cluster_slots_never_started_client_fails_at_deadline 2 clients, the second's CLUSTER SLOTS empty until 0.5 s before the deadline: exit != 0 within --test-time + 2 s FAIL at ed71bc1 (exit 0 at 7.0 s) pass
test_cluster_slots_bad_reply.py::test_rejected_auth_once_at_deadline_fails_the_run standalone, drop at request 2700, the reconnect's AUTH rejected once at the deadline: exit 1 (pins the AUTH vs CLUSTER SLOTS split) FAIL with m_slots_rejected = true (exit 0) pass
test_never_started_client_dropped_after_deadline_does_not_restart 2 clients, one never answered, both dropped after the deadline by a server that then answers: exit 0, JSON Total duration ≥ --test-time − 100 ms (the late client is timed from the run's start). Under TLS the held connection never completes its handshake, so its drop is a failed connect and the run fails (exit 1, asserted) pass at ed71bc1 pass; FAIL with the set_start_time_at() branch disabled (1501 ms)
test_never_started_client_dropped_before_deadline_does_not_restart same, dropped 0.5 s and 0.05 s before the deadline: exit 0 within --test-time + 2 s and the same duration check (TLS: exit 1, as above) FAIL at a785c63 (7.0 s / 6.0 s) and at ed5fd89 (exit 1) pass; FAIL with the branch disabled (7.0 s)
test_connection_setup_rejected.py::test_replica_rejecting_auth_with_reconnect_is_closed_not_redialed replica rejecting AUTH under --read-preference primary, --reconnect-on-error unlimited and --max-reconnect-attempts=50, --test-time 2: exit 0, one "closing it" per connection, no redials, 2 AUTH attempts at the replica, 0 Connection Errors FAIL at 8b58d15 (10/10) pass (10/10)
test_server_gone_bounded_reconnects_fail_as_before --max-reconnect-attempts=6 and =1, server gone for good: exit 1 with "Maximum reconnection attempts (N) exceeded", no "run is over", Connection Errors ≤ 2×(N+2) FAIL at 340f797 (gave up at the deadline) pass
test_server_restarting_around_deadline_bounded_ends_normally --max-reconnect-attempts=10, server down at 5.3 s and back at 6.8 s, --test-time 6: exit 0 FAIL at 340f797 (exit 1) pass
test_setup_recovered_then_server_gone_ends_normally AUTH rejected, then accepted with replies (default workload, and error replies only), then the server goes: exit 0, no "setup failed earlier" FAIL at 340f797 (error-reply variant exit 1) pass
test_setup_rejected_then_server_gone_fails_the_run AUTH rejected on reconnect, then the server goes away: exit 1 with "Connection setup failed earlier and the run is over" FAIL at b8267b0 (exit 0, 2/2) pass 2/2
test_server_gone_unlimited_reconnects_warn_with_requests -n 1000000: still waiting, and the 10-attempt warning (backoff factor 0.7 with the 0.1 s floor: the 10 attempts take about 2.5 s, so the warning shows about 4.5 s in; the test waits 8 s, plus 5 s under sanitizers, instead of 18 s) pass pass
test_server_gone_before_start_..._end_at_stage_timeout dead from the start: exit 2 at --connection-stage-timeout, and --test-time did not end it (fails with the steady-state gate removed, M6b) pass pass
  • Prior full validation at 9d78591: make check 13/13, reconnections 31/31, setup rejection 12/12, cluster-slot errors 20/20, retry-on-error 28/28, run-count exit codes 9/9, JSON output integrity 25/25, and an oss-cluster subset 21/21. Earlier revisions also passed the reconnection suite under TLS; see the review comments for the full historical evidence.
  • Final owner delta: make check 13/13; both new replica regressions 10/10; setup-rejection and cluster-slot suites 34/34, focused bounded/rejected/recovered/startup reconnect cases 5/5.
  • Unix-socket branches were exercised manually against a private unix-socket Redis: --test-time 4 --reconnect-backoff-factor 20 exited 0 after 5.0 s with Total duration 4000 ms (before: 22 s, 21558 ms). No CI cell runs --unix yet (follow-up CI: run the RLTest suites over a unix socket (--unix) #726).

Fixes #656

Test hardened for this PR

test_json_output_integrity.py::test_json_aggregated_connection_errors_sec killed every normal client every 0.25 s. Under TLS its kills could land on all of a client's connections during their handshakes, so that client never got a reply. This PR (correctly) fails a run with a client that never ran, so that run exited 1. The test is about Connection Errors/sec, not that rule: its kill loop now only kills connections that have served a workload command (cmd get/set). Its assertions are unchanged. Measured: 12/12 under --tls, 10/10 under --tls with taskset -c 2, 5/5 plain.

Merge-order notes

Final owner pass

  • secondaryPreferred + a rejecting replica + --reconnect-on-error: exit 0 in 10/10 runs, exactly one close per replica connection, no redials, and zero Connection Errors. The previous owner reproduction was nondeterministic (exit codes 1, 0, 1, 0, 1).
  • Required secondary + a rejecting replica: exit 1 in 10/10 runs. The client checks unrecovered setup rejections when a sibling ends it, so scheduling no longer decides the result for unlimited reconnects.
  • The started-client wall-clock gate is covered by a deterministic unit test. Removing the gate fails its bounded/no-reconnect assertions; gating never-started clients too fails its two Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667 assertions.
  • TLS chaos compatibility decision: a client that never completed a request makes that run invalid, including when its TLS handshake is repeatedly killed. The previous owner's CLIENT KILL-every-10-ms experiment with --tls --run-count 3 --test-time 2 returned exit 1 in 4/9 runs in 14–15 s on this PR; master returned exit 0 in 21–26 s, overshooting every deadline. The failure policy is intentional. The Connection Errors/sec test only kills connections that have served GET/SET so it measures that rate; the never-started-client tests independently pin the failure policy.

Final deadline and drain correction

Required-replica setup failures now pass through one completion helper for normal, deadline and drain-grace endings. A deterministic fixture covers primary/secondaryPreferred/secondary across all three routes. Independent scripted-peer reproduction: an unrecovered required replica plus a stalled primary incorrectly returned 0 before the drain correction and returns 1 afterward (~13 seconds in both). Final validation: unit 13/13, setup/reconnect 16/16, drain 5/5. README-only d7f430f adds the explicit compatibility policies; runtime code remains ee56133.

Affected versions

2.3.0 through 2.5.2 (latest) and master.

  • Introduced by 7ae9f65 "Connection Error Tracking & Auto-Reconnect" (Connection Error Tracking & Auto-Reconnect #320), which added --reconnect-on-error with unlimited attempts as the default. The first tag that contains it is 2.3.0.
  • Confirmed by running: master 3b3c2d7 and 0298524, and 2.5.2 (per the issue).
  • Derived from history: 2.3.0 through 2.5.1.

🤖 Generated with Claude Code


Note

High Risk
Touches core reconnect, deadline, cluster multi-shard teardown, and exit-code paths; mis-coordination could hang runs, end early, or mis-report failures.

Overview
With --reconnect-on-error and unlimited --max-reconnect-attempts, a --test-time run that has started now stops reconnecting at the deadline, prints its report (exit 0), and caps stats duration at --test-time instead of retrying forever or waiting out full backoff past the deadline. Reconnect delays are capped to time left before the deadline; failed connects after the run ends are no longer retried, while one post-deadline reconnect remains for a dropped live connection so held requests can replay.

shard_connection centralizes deadline/stop logic (run_deadline, run_is_over, stop_reconnecting_if_run_over, siblings_done / end_client_after_run) so multi-connection clients are not torn down while a sibling still has a pending reconnect or replay queue. Setup rejections (AUTH/SELECT/HELLO) since the last workload reply still fail the run (#630); optional replicas under primary or secondaryPreferred + reconnect are closed without endless redials. Unix-socket failed dials in reconnect() now count as connection errors like TCP. Finite --max-reconnect-attempts behavior is unchanged.

Docs (README, man page) describe the policy; memtier_benchmark threads report count_failed_clients(); cluster shard dials after the run leave the connection dead instead of ending the whole client mid–CLUSTER SLOTS. Added reconnect_siblings_test and broad integration tests (proxy fixtures, cluster slots, setup rejection).

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

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

Thanks for this. The core fix for #656 is real: a --test-time run that loops forever with no report is a bad bug. Capping the backoff on a local copy in next_reconnect_delay(), so the schedule itself doesn't change, is a nice touch. I've gone through most of the diff, and I have a few comments below. Most of them are about how much this PR changes at once, not about the main fix.

  1. Exit-code compatibility for never-started clients. Before this PR, a TLS chaos run where repeated kills meant a client never got a reply returned exit 0, though it overshot the deadline. Now it returns exit 1. You had to change test_json_output_integrity.py::test_json_aggregated_connection_errors_sec to kill only connections that have already sent GET/SET, and I think that's a sign users with similar CLIENT KILL / failover setups will see this too. You do call it out as intentional, but writing it down isn't the same as choosing the less-breaking fix (the reasoning from PR#212 and PR#60). Did you consider a less-breaking option? One would be to stop such a client at the deadline, print a warning and keep exit 0, and save the failure for a later change if we want it. Fixing the hang doesn't seem to need the stricter exit code.

  2. secondaryPreferred now depends on --reconnect-on-error. If I read reconnect_after_setup_error() right, a replica whose AUTH is rejected under secondaryPreferred is closed as unneeded only when --reconnect-on-error is set. Without that flag, End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750's handling still applies. So one flag silently changes how another behaves, and the README needs a whole paragraph to explain the split. Could the rule be the same either way? Or could this replica-policy change go in its own PR, since it isn't really about the deadline?

  3. Scope. Besides the deadline fix, this PR also changes unix-socket Connection Error counting (the numbers in reports change), the secondaryPreferred replica policy, the one-time --requests warning, the CLUSTER SLOTS refresh after the deadline, and how client::fail_run() / count_failed_clients() decide whether a run failed. I think each of these is fine on its own. Together they make a high-risk change to the reconnect path that's hard to bisect if something regresses. Would you be open to splitting out at least the unix-socket counting and the replica-policy change?

  4. State flags in shard_connection. This adds m_connect_failed, m_dropped_after_run_over, m_setup_failed_since_reply, m_slots_rejected and m_slots_retried_after_run_over. They sit next to the existing m_setup_rejected, m_setup_rejected_replica and m_reconnecting, and some of them are deliberately never cleared. The header comments already have to explain which flag is set before attempt_reconnect() so a later check sees it. Did you consider a small enum for "why the last connect ended" plus one "post-deadline reconnect used" bit? That would make the possible combinations easier to reason about (the "all errors through one path" point from PR#320).

  5. Timing-sensitive tests. Several of the new tests rely on tight wall-clock windows: drops at deadline −0.05 s, moved_after=2700 at --rate-limiting=1000 to land in the last 300 ms, and Total duration within +500 ms. Given the recent flake fixes (Fix the TLS flake in the Prometheus latency cross-check (F13) #743, Bound the CLIENT KILL loop in test_ask_retry_waits_for_reconnect #758), I'm a bit worried about these on loaded CI runners, especially the TLS and sanitizer jobs. _SLACK covers sanitizers, but how many repeated runs have these had on the slower jobs?

Nitpicking: the test helpers set proxy._forward_first_only directly from outside the class. A small setter would read better. Also, the follow-up commits have empty bodies, which makes it harder to see later why each one was needed.

I think the core fix is solid. I'd mainly like answers on the exit-code compatibility (1) and the coupling between the two flags (2) before this merges. Since this touches cluster reconnects, it may be worth a second look from whoever owns cluster routing.

@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.

Stale Bugbot comment from a previous run.

Comment thread shard_connection.cpp Resolved
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the first-pass review. Point by point:

  1. Server gone before the first reply. Yes, that's intended. finished() means nothing until the run has started: before the first reply the start time isn't set, so the duration would read as huge. Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630 uses the same get_reqs_answered() guard for the same reason. Changing what happens at startup is out of scope for --reconnect-on-error with unlimited --max-reconnect-attempts: run never ends when the server stays down, even past --test-time #656, but I've documented the limit in the README section (5f95ad7): "The run's clock starts with its first reply, so a server that is gone before that is retried indefinitely too."

  2. Exit 0 vs. Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630's fail. Agreed, this needs a maintainer to confirm, which is why it's the first item in the PR description. To restate the split: a server that rejects setup (AUTH/SELECT/HELLO) at the deadline fails the run, as in Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630, because that is a configuration problem. A server that is unreachable at the deadline ends the run normally, because the requested window was measured and the connection errors are in the report. The second commit of Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630 also shows the alternative's cost: leaving the client unended after a reconnect near the deadline made test_json_aggregated_connection_errors_sec and test_F17_monotonicity_under_churn fail. If maintainers prefer exit 1 here, it's a one-line change (drop set_end_time() in stop_reconnecting_if_run_over()).

  3. Cluster mode, siblings not idle. In that case the check disconnects only this connection and returns. Siblings with replies pending end the client through the existing finished() && all_connections_idle() checks in process_response()/handle_timer_event(), whose disconnect_all() also frees any pending m_reconnect_timer, this connection's included. That is the same flow as the existing Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630 branch. A cluster shard connection doesn't reconnect by itself either: cluster_client::reconnect() leaves it to a CLUSTER SLOTS refresh over the main connection, which goes through the same attempt_reconnect() check when it fails. I'll leave the CI cluster cells to cover this; the new tests still skip cluster because the proxy only fronts a single node.

  4. Nit: warning with --requests and --test-time both set. That combination can't happen: memtier rejects it at parse time (Error: --test-time and --requests are mutually exclusive.). The only other place requests is set is when --test-time is absent (the default of 10000, or --requests allkeys). So the wording is accurate whenever it fires.

Also in 5f95ad7: Bugbot's stale-duration finding (inline reply). The synchronous reconnect() < 0 path now counts the failure and advances the clock, and the tests also cover a unix socket.

@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch from 5f95ad7 to f189250 Compare October 16, 2026 08:11
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Rebased onto master cf5753f (f189250). The --help conflict with #668 is resolved by keeping both sides.

A correction to point 1 of my earlier reply: a server that is gone before the first reply is not retried indefinitely. The --connection-stage-timeout supervisor (#426) bounds that startup phase and aborts the run with exit code 2 once it expires (30 s by default). This is also what the reviewer of #668 saw with a server dead from the start: --test-time=4 --reconnect-on-error --reconnect-backoff-factor=0 was still reconnecting at 30 s, and then exited 2 at 30.0 s. --test-time cannot apply to that case because the run's clock never starts, and #426 already handles startup failures on their own timeout. So I've left the behaviour unchanged. In f189250 I fixed the README sentence and added test_server_gone_before_start_unlimited_reconnects_end_at_stage_timeout, which checks the exit code and timing (5/5 passes, exit 2 at 3.0 s).

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the current version of the first-pass review. I pushed c6e43b0, rebased onto master 0062e56.

  1. Bounded --max-reconnect-attempts. Yes, this is intended, and the PR description, README, --help and man page now say so. Once the run has started, the --test-time deadline wins even with attempts left. A run whose attempts are not exhausted by the deadline ends there with exit 0. On master, --test-time=6 --max-reconnect-attempts=10 with the server killed at 2 s exited 1 at 13 s; it now exits 0 at 6 s. A run whose attempts run out before the deadline still fails with exit 1, as before. The README sentence you quoted now reads "when they are exhausted before the run's stop condition, the thread ends and the run fails (exit code 1)". test_server_gone_bounded_reconnects_deadline_wins pins both cases. If maintainers would rather keep exit 1 for bounded attempts, gating the check on max_reconnect_attempts == 0 is a one-line change.

    This push also fixes a bigger problem in the same path. A reconnect armed just before the deadline still waited its full backoff delay (up to 60 s), and the report counted that dead time. For example, --test-time 4 --reconnect-backoff-factor 20 exited after 22 s with Total duration 21831 ms. The armed delay is now capped at the time left; the cap applies to a local copy, so the backoff schedule is unchanged. A started client's stats end at the deadline, so the same run now ends at 4000 ms. test_server_gone_long_backoff_ends_at_test_time covers it.

  2. Unix-socket connection-error counts. Nothing in the repo depends on the old, lower number. The tests that check Connection Errors run over TCP, where every failed connect was already counted. The new count (1 drop + N failed connects) matches TCP. It is called out in the PR description and README, and test_server_gone_unlimited_reconnects_honor_test_time checks that failed connects are counted, on both transports. CI has no --unix cell, so the PR body records that gap. I checked the unix path by hand against a unix-socket redis.

  3. Cluster with a fully dead cluster. It is bounded the same way. A shard connection's reconnect() returns 1 and leaves the work to a CLUSTER SLOTS refresh over the main connection, and the main connection's failed connect goes through the same check. I measured a 3-node cluster with all nodes killed at 2 s (--cluster-mode --test-time 5 --reconnect-backoff-factor 4). Before this push it exited 0 at 7.0 s with a 6013 ms duration; now it exits 0 at 6.0 s with 5000 ms. A related edge case: when a dial in a slot-map refresh fails after the run is over, the connection is now left dead instead of ending the client from inside the CLUSTER SLOTS handler. All of test_reconnections.py passes on oss-cluster --shards-count 3.

Nit: done. The comment on warn_reconnecting_indefinitely() now says it only applies to --requests. A --test-time run is ended at its deadline by stop_reconnecting_if_run_over(), and honor_test_time asserts that the warning does not appear there.

Other changes in this push, from review:

  • Only a failed connect stops the reconnects. A drop from a live server around the deadline is still reconnected, so held replays are resent. This replaces the m_reconnect_attempts == 0 rule.
  • A client that never got its first reply, while another client did, is now timed from the benchmark's start. This uses the same run_is_over() contract as Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667.

@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch from 5bad025 to 13b0995 Compare October 16, 2026 10:41
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Pushed 13b0995, rebased onto master 1d13ff5 (#661, #694). I resolved the shard_connection ctor/member conflict with #661's m_thread_end_reason by keeping both.

  • Endless reconnect loop past the deadline, fixed. It happened with --retry-on-error and a peer that accepts each connection and closes it at once (a proxy with a dead backend, a TLS terminator, maxclients). Once the run is over, a dropped established connection now gets exactly one reconnect, which resends the held replays. A second drop counts as a failed connect and ends the run. New test test_accept_then_close_peer_with_retry_on_error_ends_at_test_time: at c6e43b0 it was killed at 21 s with no report; now it exits 0 at 5.0 s for --test-time 4. The CLIENT KILL loop repro still shows 0/5 "run is over".
  • Never-started client: it stops at the deadline but did no work, so the run exits 1 ("client(s) stopped before reaching their stop condition"). The README now documents this, and the test asserts it.
  • m_connect_failed is now cleared when a connection is established. I also reflowed the README --test-time bullet.
  • run_is_over() and connection_stage_steady_state_reached() keep Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667's contract, so whichever PR lands second dedupes them, including the unit-test stubs.

Local checks: all of test_reconnections.py (20 tests), make check (7/7), and test_retry_on_error.py:test_dropped_replays_are_counted_as_errors (3/3).

@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch from 13b0995 to c2c86b5 Compare October 16, 2026 11:11
@fcostaoliveira

fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Replying to the review as edited at 10:43. I pushed c2c86b5, rebased onto master 8f5fa34 (#669).

  • Exit code with a finite --max-reconnect-attempts. No change from before, restated here. Once the run has started, the --test-time deadline wins. A run that still has attempts left at the deadline ends there with exit 0. A run that uses up its attempts before the deadline still exits 1. Master exits 1 in both cases: with --test-time=6 --max-reconnect-attempts=10 and the server killed at 2 s, master exits 1 at 13 s and this branch exits 0 at 6 s. This is the first item in the PR description. The alternative is a one-line gate on max_reconnect_attempts == 0. I agree a maintainer should pick one explicitly, and that it belongs in the release notes either way.

  • An enum for the four flags. I'd rather not. The four flags don't describe one "reason" with a single lifetime:

    • m_reconnecting means a timer is pending.
    • m_setup_rejected marks the pending reconnect as following a rejected AUTH/SELECT/HELLO, and is cleared when the timer fires.
    • m_connect_failed describes the last error and is cleared when a connection is established.
    • m_dropped_after_run_over is set once per connection after the deadline and stays set for the rest of the run.

    Combining them would need an enum plus separate bits for the ones that outlive a single reconnect. Instead I made the ordering explicit in comments: the m_setup_rejected pre-set now explains why it is still needed (a second drop after the deadline counts as a failed connect).

  • Sanitizer timing. Fixed. A module-level _SLACK (5 s under ASAN/TSAN/UBSAN, else 0) widens every wall-clock bound in the new tests. The Total duration upper bound goes from +500 ms to +2000 ms under sanitizers. Its lower bound (−100 ms) and the overshoot it guards against (a 20 s backoff) are far apart, so it still discriminates. The stage-timeout test now uses the shared _sanitized().

  • Overlap with Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667. Agreed. Both PRs implement the same run_is_over() / connection_stage_steady_state_reached() contract, including the unit-test stubs. Whichever lands second dedupes them.

  • Nit, duplicate update_connection_error. Done. shard_connection::count_connection_error() is now used by both attempt_reconnect() and the reconnect() < 0 branch.

Also in this push:

  • New test test_live_server_drop_after_deadline_resends_held_requests. A proxy holds the replies from just before the deadline, then drops the connections just after it. With --retry-on-error the drop is reconnected and the held requests are answered, with none abandoned (5/5 runs).

  • Mutation results. Each mutant fails the test that targets it:

    Mutant Fails
    Removing the !m_connect_failed gate (M5) the new test
    Forcing m_connect_failed to true (M5b) the new test
    Removing the steady-state gate in run_deadline() (M6b) the dead-from-the-start test, which now asserts --test-time did not end it
    Warning without --requests (M11) honor_test_time, which now makes more than 10 attempts
  • End a --test-time run whose server stops replying (10 s drain grace) #669 interaction. test_test_time_reconnecting_client_left_alone asserted that a 15 s backoff ran past the grace period. With this PR the reconnect is due at the deadline, so the run now ends well before the grace period. I updated that assertion. The test still checks that the reconnecting client is not reported as stalled. Correction: after that change it no longer reaches the grace window, so it did not test End a --test-time run whose server stops replying (10 s drain grace) #669's idle-client guard any more (mutant G1 survived). b28ac5a adds test_reconnect_hanging_past_drain_grace_is_left_to_connection_timeout, which does.

  • Accept-and-hold peer (accepts connections, never replies) with --retry-on-error. The run ends through End a --test-time run whose server stops replying (10 s drain grace) #669's drain grace: exit 0 at about --test-time + 10 s, with the "got no reply" warning. This holds whether the proxy just stops forwarding, or drops the connections and then holds the new ones.

Local checks:

Suite Result
test_reconnections.py 21/21
test_retry_on_error.py 23/23
test_test_time_stalled_server.py, standalone and on oss-cluster 4/4 each
make check 7/7

@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.

Stale Bugbot comment from a previous run.

Comment thread shard_connection.cpp Resolved
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Pushed b28ac5a, on master 971776c (#667). I squashed the branch into one commit because rebasing the old series onto #667 conflicted at every step.

Local checks: test_reconnections.py 21/21, test_retry_on_error.py 23/23, test_test_time_stalled_server.py 4/4 standalone and 4/4 on oss-cluster, make check 7/7.

@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.

Stale Bugbot comment from a previous run.

Comment thread shard_connection.cpp Resolved
@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch from b28ac5a to 692186c Compare October 16, 2026 12:09
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
Over TLS a refused connect logs "TLS connection error: (null)" rather
than "Connection error: ", so the mixed test counted 0 logged errors
and failed in the TLS CI cells. Count the "attempting reconnection" and
"Maximum reconnection attempts" lines instead: attempt_reconnect() logs
exactly one of them for each error it counts, on every transport. Run
the dead-server tests with the TLS args too.

Bound the count from above (clients x (1 + attempts), or the logged
count plus one per client for the Ctrl+C run) and check that every
Time-Serie second is within the run, so merging the epoch-keyed buckets
of a never-started client again is caught.

Give the mixed run an 8 s --test-time, so client 0's last dial at 6 s
happens before the deadline also when reconnects stop at the deadline
(#666), and expect the run to last at least 5 s: it ends when that dial
fails, and averaging in client 0's zero timestamps halves it to 3 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch from 692186c to f496ced Compare October 16, 2026 12:58
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the first-pass review as rewritten at 12:10. The new head is f496ced. It also fixes both Bugbot findings; I replied to those inline.

  1. Exit code with a finite --max-reconnect-attempts. The decision stands as written in the PR description:

    • Once the run has started, the --test-time deadline wins.
    • A run that still has attempts left at the deadline exits 0 with its report.
    • A run that uses up its attempts before the deadline still exits 1.

    You're right that --reconnect-on-error with unlimited --max-reconnect-attempts: run never ends when the server stays down, even past --test-time #656 is only about the unlimited case. The alternative is a one-line gate: apply the deadline stop only when max_reconnect_attempts == 0, and leave bounded runs as on master. I'm happy to switch to it. I'm leaving the choice to a maintainer rather than making it myself.

  2. Flag state. Yes, this is intentional: each connection gets exactly one reconnect after the deadline, ever. The header now says so: "Never cleared: each connection gets one reconnect after the deadline." I'm declining the single-state refactor, for the reason I gave on the earlier version: the flags have different lifetimes.

    • The pending timer.
    • The reason for the current reconnect.
    • The kind of the last error, cleared on connect.
    • The per-connection, post-deadline latch.

    Folding them into one enum would still need side bits.

  3. Unix-socket Connection Errors. Agreed. The PR description now has a release-note line for it, next to the exit-code one. The --unix CI step stays a follow-up.

  4. Wall clock vs. the monotonic timer. That is the worst case: one extra failing reconnect.

    • The timer is armed for the time left on the wall clock. If it fires a little early by gettimeofday(), the pre-check in handle_reconnect_timer_event() sees the run as not over yet and makes one more attempt.
    • When that attempt fails, attempt_reconnect() arms the next timer for the few microseconds still left.
    • That timer finds the deadline passed and stops, with the stats ended at the deadline itself.
    • The cost is one more counted connection error and a microsecond-scale delay; the reported duration is unaffected.
    • If the wall clock is stepped backwards during the run, the time left is recomputed for each timer, so it delays the stop by the size of the step, not more.

fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
Over TLS a refused connect logs "TLS connection error: (null)" rather
than "Connection error: ", so the mixed test counted 0 logged errors
and failed in the TLS CI cells. Count the "attempting reconnection" and
"Maximum reconnection attempts" lines instead: attempt_reconnect() logs
exactly one of them for each error it counts, on every transport. Run
the dead-server tests with the TLS args too.

Bound the count from above (clients x (1 + attempts), or the logged
count plus one per client for the Ctrl+C run) and check that every
Time-Serie second is within the run, so merging the epoch-keyed buckets
of a never-started client again is caught.

Give the mixed run an 8 s --test-time, so client 0's last dial at 6 s
happens before the deadline also when reconnects stop at the deadline
(#666), and expect the run to last at least 5 s: it ends when that dial
fails, and averaging in client 0's zero timestamps halves it to 3 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch 2 times, most recently from 6831520 to 9718017 Compare October 16, 2026 13:37
…rver

With --reconnect-on-error and unlimited --max-reconnect-attempts (the
default), a server that goes away for good kept a --test-time run
reconnecting forever, with no report: the reconnect path never checked
whether the run was over, and finished() is otherwise only checked when
requests are sent or answered. A reconnect armed before the deadline also
waited out its full backoff (up to 60 s) and the report counted that dead
time.

With unlimited attempts:
- run_deadline() / run_is_over(): the stop condition in one place. A
  started client is timed from its own start, also on the wall clock
  (within 1 ms counts as passed); a client that never started, once any
  connection reached steady state, from the benchmark's start (#667's
  contract, which this extends).
- next_reconnect_delay() caps the armed delay at the time left, on a local
  copy, so the backoff schedule is unchanged.
- stop_reconnecting_if_run_over(): when a connect fails and the run is
  over, stop reconnecting; a started client's stats end at the deadline,
  so the duration and throughput reflect --test-time (exit 0).
- A setup rejected since the connection's last workload reply (any reply,
  errors included) still fails the run (#630), also when the server then
  goes away.
- A client whose first connection completes only after the run is over
  is timed from the benchmark's start, so it ends at its first reply.
- A drop of an established connection after the run is over gets one
  reconnect, so held replays are resent; a second drop counts as a failed
  connect (a peer that accepts and closes would otherwise loop at 0 s).
- The client is ended only once its other connections are done too
  (siblings_done()), e.g. another cluster shard with a reconnect pending;
  a failed run is recorded on the client (fail_run()) so a sibling's later
  normal end still fails it.

With a finite --max-reconnect-attempts nothing changes: the attempts bound
the run at their usual backoff, and running out of them fails it (exit 1),
as before.

Also: failed reconnects that never reach the event loop (unix socket), and
failed cluster dials after the run is over, are counted as connection
errors (count_connection_error()); with --requests and unlimited attempts,
memtier warns once per process, after 10 failed attempts, that the server is
still unreachable; a
cluster dial that fails after the run is over leaves the connection dead
instead of ending the client inside the CLUSTER SLOTS handler. README,
--help and the man page document the behaviour; tests in
test_reconnections.py and tests/unit/reconnect_siblings_test.cpp cover it.

Fixes #656

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-656-reconnect-deadline branch from 8b58d15 to 9d78591 Compare October 17, 2026 03:42
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the review as edited at 02:49. I pushed 9d78591, on master 3fcbfbc (#757, #761).

  • Item 2, splitting off the unix-socket Connection Errors change and the --requests warning. I'd rather keep both here.

    • The unix-socket change isn't independent of the deadline fix. Counting a failed connect inside reconnect() is what moves the run's clock on that path, and finished() reads that clock. The new run-over checks also run in the same reconnect() < 0 branch. Without the count, a unix-socket run would still never see its deadline. So it is part of the same accounting fix, on the same counters and paths that Reconnect when AUTH/SELECT/HELLO fails on a reconnect instead of hanging #630/--reconnect-on-error with unlimited --max-reconnect-attempts: run never ends when the server stays down, even past --test-time #656 changed. Splitting it out would leave an intermediate state where TCP and unix sockets report different counts for the same outage. The release notes list it on its own line, so it stays visible.
    • The --requests warning is small: one function, warning once per process. It belongs to the same decision the PR documents for unlimited attempts. A --test-time run now ends at its deadline, and a --requests run keeps waiting by design, so the warning is how that wait is made visible. Without it, --requests would be the one unlimited-attempts case that still looks like a hang.
  • Item 4. Done. shard_connection.h now says that siblings_done() and end_client_after_run() are public only for tests/unit/reconnect_siblings_test.cpp.

  • Item 1, exit code for a server that is gone. This hasn't changed since earlier rounds. With unlimited attempts, a --test-time run that measured its window ends with exit 0, and the connection errors are in the report. Runs that fail are:

    • a client that never ran;
    • a setup rejected since the last reply;
    • bounded attempts that run out.

    The PR description states this decision first, so a maintainer can still flip it.

  • Item 3, the flags. Same answer as before: they have different lifetimes, so I'm not folding them into one enum here.

Also in this push:

  • Interplay with End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750, fixed. With --reconnect-on-error, a replica whose AUTH is rejected under --read-preference primary was redialed about once a second and rejected each time. Its retry fell due at the deadline, and the rejected-setup check then failed a run that the primaries had served in full. It is now closed and left closed, as End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750 does without --reconnect-on-error; connections that are needed keep reconnecting.

    Check 8b58d15 9d78591
    New test test_replica_rejecting_auth_with_reconnect_is_closed_not_redialed (unlimited, and --max-reconnect-attempts=50) fails 10/10 passes 10/10
    Real 3+3 cluster, requirepass on the primaries only, --cluster-mode -a dummypw -t 2 -c 2 --reconnect-on-error --test-time 3 rc 1 in 3/4 runs, 36 redial lines each rc 0 in 4/4 runs, no redials
  • test_json_aggregated_connection_errors_sec hardened. Its 0.25 s kill loop could hit every connection of a client during its TLS handshake. That client never ran, so the run (correctly) failed. The loop now only kills connections that have served a workload command, and the Connection Errors/sec assertions are unchanged. Results: 12/12 under --tls, 10/10 under --tls with taskset -c 2, 5/5 plain.

Local checks:

Suite Result
make check 13/13
test_reconnections.py 31/31
test_connection_setup_rejected.py 12/12
test_cluster_slots_bad_reply.py 20/20
test_retry_on_error.py 28/28
test_run_count_exit_code.py 9/9
test_json_output_integrity.py 25/25
oss-cluster subset 21/21

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the review as edited at 2026-10-17 03:44:00Z. Pushed 705b429 as a normal fast-forward on 9d78591.

(2) Bounded runs: done. For a started client, run_is_over() adds the wall-clock check only when stops_at_deadline() holds (reconnect enabled, unlimited attempts). Bounded and no-reconnect runs keep master's stats-clock check. The never-started-client benchmark deadline remains as on master (#667); gating that path too would regress it. A deterministic unit test covers all these cases. Removing the started-client gate fails two assertions, and gating never-started clients too fails two different assertions.

(3) TLS kills and the narrowed accounting test: yes, this is a deliberate user-visible behavior change. A client that never completed a request makes that run invalid. It must not silently contribute no work while other clients determine the result. The previous owner's experiment with CLIENT KILL every 10 ms and --tls --run-count 3 --test-time 2 recorded exit 1 in 4/9 PR runs taking 14–15 s; master returned exit 0 in 21–26 s, overshooting every deadline. We are keeping that failure policy and have made it explicit in the PR body. The Connection Errors/sec test kills connections after GET/SET so it exercises the accounting contract; the separate never-started-client tests pin the failure contract, including TLS.

The final replica compatibility pass is also included:

  • secondaryPreferred + --reconnect-on-error: a replica rejecting AUTH/SELECT/HELLO is optional, closes once, and is not redialed; reads use the primary. Without reconnect, existing behavior remains.
  • secondary: the replica remains required. An unrecovered setup rejection is recorded even when a sibling ends the client first, so that scheduling no longer decides the exit code for unlimited reconnects.

Validation on the final delta:

  • make format-check, pre-commit staged format check, and make check: 13/13.
  • test_connection_setup_rejected.py + test_cluster_slots_bad_reply.py: 34/34.
  • Focused bounded-reconnect failure/recovery, rejected/recovered setup, and pre-start timeout cases: 5/5.
  • New secondaryPreferred regression: 10/10, exit 0, exactly one close per connection, no redials, zero Connection Errors.
  • New required-secondary regression: 10/10, exit 1.
  • Both wall-clock gate mutants fail as described above.

I also trimmed the obsolete rebase-by-rebase validation history in the PR description while keeping the before/after table and compatibility decisions. CI and an independent focused recheck of this delta are still required before merge.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

On the latest review:

  1. The secondaryPreferred behavior is intentional and is now described under the release notes and owner validation. A replica that explicitly rejects AUTH/SELECT/HELLO during setup is unusable for these credentials/configuration; the available primary still satisfies secondaryPreferred. Redialing that rejected optional connection had made otherwise successful runs exit nondeterministically at the deadline (1,0,1,0,1), whereas master returned 0. We considered retaining redials and suppressing only the final failure, but that retains needless rejected setup traffic and state-machine complexity. We chose the same close-and-mark policy as the unneeded-primary-read replica rule; secondary remains required and fails deterministically. The warning is retained so the fallback is visible. “Not needed” means not required to serve this read preference, not that replica reads were never preferred. Without reconnect enabled, the existing End a run whose connection setup is rejected instead of hanging (cluster replicas, failover, standalone) #750 behavior remains unchanged.

An independent recheck now covers AUTH, SELECT and HELLO rejection for both modes: all 18 cases passed; preferred replicas stayed closed across 730–2194 topology refreshes with exit 0/no redials/zero Connection Errors; required replicas exited 1.

  1. Yes, the TLS chaos exit 1 is deliberate: a client that never completed a request makes the run invalid. The description records the prior experiment (PR 4/9 exit 1 in 14–15s; master exit 0 in 21–26s while exceeding deadlines) and the targeted test-killer change. I have also placed the policy explicitly in Release notes.

  2. A reconnect-reason enum is a reasonable separate refactor, but these flags represent independent facts/lifetimes, so replacing them safely is larger than this deadline correction. Keeping that non-blocking cleanup separate.

  3. Unix-socket automation remains tracked in CI: run the RLTest suites over a unix socket (--unix) #726. The wall-clock test remains on reconnect/setup/teardown paths, not on ordinary request completion. The new unit regression pins started bounded runs versus never-started deadline protection.

The squash message will describe final behavior rather than the intermediate owner-commit history.

@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 705b429. Configure here.

Comment thread client.cpp Resolved
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the review edited at 2026-10-17 05:16:21Z. Pushed d7f430f, a README-only clarification; the reviewed implementation remains ee56133.

1. Replica setup policy. I considered both alternatives. Applying the optional-replica rule without --reconnect-on-error would also change #750's existing no-reconnect setup-failure behavior. Keeping that behavior with reconnect enabled leaves the repeatedly rejected replica being redialed until its deadline retry fails a run served by the primary—the compatibility regression reproduced earlier. The chosen compromise is deliberately narrow: with reconnect enabled, secondaryPreferred treats this rejected replica as optional and closes it; without reconnect, its existing behavior stays. This handles the regression introduced by the new deadline behavior in the same PR rather than landing the deadline change while leaving that case broken.

You are right that nearest can fall back to a primary. There is no technical claim otherwise. We are not generalizing the optional-setup exception to every routing mode capable of fallback: secondary can also be configured to fall back to primary, yet its replica is still required for setup validation. nearest retains its existing setup-failure policy in this change. That is a scope and compatibility decision, not a limitation of its routing implementation. The README Read Preference section now spells out the exception and explicitly says that secondary and nearest do not get it; the reconnect section refers back to those exceptions. A broader change to which configured endpoints may fail setup silently should be reviewed as a routing-policy change of its own.

2. Partial thread success was considered and ruled out. Counting an entirely never-started client as an error but returning success when any sibling did work would accept a different workload from the one requested. For example, -t 1 -c 4 could succeed with one working client and three clients that never completed a request. More problematically, that rule makes validity depend on how clients are grouped into threads: with the same four client histories, -t 4 -c 1 would fail while -t 1 -c 4 could succeed. A warning or error counter would disclose the missing work but would not make the requested client workload complete. We are keeping the per-client validity rule rather than making one successful sibling sufficient for its thread.

The 4/9 TLS-chaos result is therefore not a promise of a constant exit code under scheduler-dependent CLIENT KILL timing. Different kills can prevent a client from ever receiving a reply, or allow all clients to start. The run fails in the former case; other successful clients do not override it. Master obtained success by allowing runs to continue well past their deadlines. The separate never-started-client tests pin the chosen failure rule; the narrowed Connection Errors/sec test measures accounting after clients have actually started. The README now explicitly includes repeated TLS-handshake failures and says that work by other clients on the thread does not turn that run into success.

4. Confirmed. The shared normal/deadline/drain completion scan can set m_run_failed only when both stops_at_deadline() and setup_failed_since_reply() hold. The former is exactly reconnect_on_error && max_reconnect_attempts == 0; without reconnect, or with any finite attempt limit, this scan cannot mark the run failed. Within that scope, a normal completion must still preserve an unrecovered required-replica setup rejection, which the nine mode/completion unit cases now cover.

For the nonblocking state-refactor suggestion, I am keeping the existing flags in this change: the recorded failed-connect/drop history, persistent setup failure since the last reply, and one-time slot-refresh retry have different lifetimes. No C++ or test code changed in this documentation pass. git diff --check passes; the existing final-code validation and independent MERGE recheck remain applicable.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks; I re-read the updated review. The compatibility alternatives were considered, not just documented:

  1. Keeping exit 0 with a warning would accept a requested client that completed no workload. We retain the explicit per-client validity decision: successful siblings do not make that client's requested work complete. The partial-thread alternative also makes identical client histories pass under -t 1 -c 4 and fail under -t 4 -c 1. The narrower Connection Errors/sec killer keeps that test about rate accounting; separate never-started-client tests pin the intentional exit behavior.
  2. A uniform secondaryPreferred rule without reconnect would change another existing mode. Keeping retries with reconnect restores the measured nondeterministic failure (1,0,1,0,1) when the primary served the workload. The scoped exception is therefore retained and now explicit in README; secondary and nearest keep their existing setup-validation policy.
  3. I considered splitting these changes. The replica exception is needed to avoid that regression when enforcing the deadline, and Unix failed-dial accounting shares the error/stop path whose behavior is tested for both transports. Slot-refresh, sibling completion, and run-failure propagation likewise prevent premature teardown or false success at the new deadline. Keeping the reviewed fixes together avoids landing an intermediate state with those known failures. The squash description will state the final behavior, including compatibility changes.
  4. An enum could improve the state representation, but replacing the independently tested state machine now would be a separate refactor. The comments identify persistent versus reset-on-reply flags; I am deferring that cleanup.
  5. I am not claiming a new large repetition campaign on slow runners. The recorded focused repetitions include live-server replay 5/5, drain-connect 4/4, optional and required replica cases 10/10 each, and the hardened rate test 12/12 TLS plus 10/10 TLS pinned to one CPU. The final code delta passed 13/13 unit, 16/16 setup/reconnect and 5/5 drain tests, plus an independent deterministic completion fixture and an end-to-end rejected-replica drain reproduction. Fresh TLS/sanitizer CI on d7f430f remains a merge gate. If it exposes timing failures, those must be resolved before merge.

The helper setter and commit-body suggestions are nonblocking. No runtime change was made in response to this latest comment; d7f430f only documents the selected policies.

@fcostaoliveira
fcostaoliveira merged commit 0bb7fc5 into master Oct 17, 2026
130 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 25, 2026
With --reconnect-on-error, a --test-time client that had sent workload
but never got a reply had its end time stamped at "now" by
client::set_end_time(): when its reconnect after the deadline completed,
or when its thread ended. Such a client takes part in the run's duration
average (#821), so Total duration came out above --test-time and Ops/sec
was deflated. With one client served normally and one whose SET was held
past a 4 s deadline, the run reported about 5000 ms and 80 ops/sec (5500
ms and 73 with --max-reconnect-attempts=5) instead of 4000 ms and 100.

Cap such a client's end at its own start plus --test-time once that is
past, as Ctrl+C after the deadline already does (#747). It uses the
client's own start, not run_deadline(), which times an unanswered client
from the benchmark's start and would inflate Ops/sec for a client that
started late. A client with replies still ends at "now", since that time
can cover replies it drained after the deadline (#870 tracks the gap for
a client whose last reply came before the deadline). A client that never
submitted stays out of the average, and --requests runs have no deadline.
end_client_after_run() keeps leaving such a client unended when its
server is gone after the deadline, so that run still fails (#666).

test_unanswered_end_time.py runs the scenario against a scripted peer,
standalone and in cluster mode, with unlimited and finite reconnect
attempts. It adds a late-started client, a client whose drained reply is
counted, and a client that gives up before the deadline.

Fixes #867

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 25, 2026
…er the deadline (#876)

With --reconnect-on-error, a --test-time client that got replies, all
before its deadline, and then stalled had its end time stamped at "now"
by client::set_end_time() when its reconnect after the deadline
completed. #869 capped this only for a client that never had a reply.
With one client served normally and one answered 20 times and then held
past a 4 s deadline, the run reported about 5100 ms and 82 ops/sec
(5600 ms and 75 with --max-reconnect-attempts=5) instead of 4000 ms.

Record when each client's last reply was read, from the timestamp
process_response() already takes, so a reply costs no extra clock read.
Once a client's own start plus --test-time is past, end it at that
deadline or at its last reply, whichever is later, and no later than
now. Replies drained after the deadline still count, but the idle time
after them does not. In cluster mode the last reply is per client,
whichever shard it came on, and setup replies do not count. This applies
to every answered client past its deadline, so a rate-limited client
that ends on a timer tick after it no longer counts that gap.
end_client_after_run() is unchanged, so a run whose server is gone
after the deadline still fails (#666).

test_answered_end_time.py runs the stalled client against a scripted
peer, standalone and with two cluster shards, with unlimited and finite
reconnect attempts; a client answered past the deadline that then idles;
and a cluster client answered late on one shard while the other shard's
reconnect setup is held. test_run_count_partial_ranking.py now expects
a run whose worker drops after its deadline to end at the deadline.

Not covered: a stalled reconnect setup can still let new requests go
out past the deadline (#879), and the interrupt, dead-server and
drain-grace end paths still cap at the deadline (#880).

Fixes #870

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 25, 2026
359d882 also stopped fill_pipeline()'s loop on the wall-clock deadline,
so a connection whose reconnect or slot map refresh came due at the
deadline, with nothing pending, sent no setup and ended its client
normally. Four tests in test_cluster_slots_bad_reply.py caught it:

- test_rejected_auth_once_at_deadline_fails_the_run (#666): the AUTH of
  the reconnect due at the deadline was never sent, so the rejected
  setup did not fail the run (#630) and it exited 0.
- test_cluster_slots_one_empty_reply_at_deadline_is_retried (#666): the
  slot map refresh due at the deadline was not sent (2 CLUSTER SLOTS
  requests instead of 3), so a one-off slot error was not retried.
- test_cluster_slots_{empty,malformed}_reply_mid_run_honours_test_time
  (#667): the refresh stopped at the deadline and the run ended
  normally, instead of failing as incomplete.

These are guarantees, not the old overcounting, and the tests are
unchanged. The loop condition is back to finished(), so setup still goes
out past the deadline; only the per-request break checks the deadline,
so no new workload does. The "done" check ends a client past its
deadline only once this connection's setup is done and its other
connections are done too (siblings_done(), not just no reply pending),
so a sibling's reconnect or refresh due at the deadline still runs.

This also restores cluster_slots_retention_test.cpp as it was: its slot
map retry past the deadline is a setup command and goes out again.

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.

--reconnect-on-error with unlimited --max-reconnect-attempts: run never ends when the server stays down, even past --test-time

1 participant