Skip to content

Report connection errors and Ctrl+C of clients that never connected - #707

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix-702-conn-errors
Oct 16, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix-702-conn-errors

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Decision: a client that never connected still counts in the final report for what it did record (connection errors, Ctrl+C), so the final Connection Errors matches the live N conn errors line. Its timestamps stay out of the merged run times, as #350 intended. No defaults or output fields change. A run where no client connected reports 0 rates, as on master, not NaN.

Problem

When the server is never reachable, the final report and the JSON show Connection Errors: 0, although the live line counts every failed connect. If the run is stopped with Ctrl+C in that state, memtier prints Interrupted by user (Ctrl+C) after 0.0 secs and the JSON has "Interrupted": "false" (see #702 and its comment). When some clients connect and a lower-index one never does, that client's errors are also missing from the total.

Root cause

A client's start time is set when its first connection is established (handle_event() -> process_first_request() -> client::set_start_time()). client_group::merge_run_stats() skips every client with !has_started(), so that the client's zeroed start time is not averaged into the merged timestamps (#350). That skip also dropped the client's connection errors and its m_interrupted flag. The Ctrl+C message averages client_group::get_duration_usec(), which skips the same clients, so it is 0.

Fix

  • client_group::merge_run_stats() merges every client. The averaging counter advances only for clients that started.
  • run_stats::merge() handles a run that never started separately. It adds only that run's connection error count, into m_cur_stats, and takes its interrupted flag, then returns. print() and aggregate_average() rebuild the totals from the per-second stats plus m_cur_stats with summarize(), which is why the errors go into m_cur_stats and not m_totals. The run's timestamps and per-second buckets are not merged: with a zero start time those buckets are keyed from the epoch.
  • totals_cmd::summarize() (also used for arbitrary commands) returns 0 rates when the duration is 0. When no client starts, print() now reaches summarize() with a zero duration, and without this guard the Sets/Gets/Waits Ops/sec and KB/sec would be -nan in the text and null in the JSON, including BEST/WORST with --run-count. The guard matches the existing ones for hits, misses and connection-error rates.
  • The Ctrl+C message uses the wall time since benchmark_start_time.

Behaviour notes

  • The Ctrl+C "after N secs" value is now wall time since the run started. It includes connection setup and, with --clients-start, the staircase ramp. On a healthy run it matches master (3.5 s for a SIGINT at 3.5 s).
  • JSON Total duration (and Start/Finish time) stays 0 for a run where no client ever connected. There is no start time to report.
  • Totals/Connection Errors includes the errors of never-started clients, but they do not appear in any per-second Time-Serie entry, because there is no start time to place them. The per-second entries have no connection-error field anyway.

Overlap with other PRs

Tests

Three tests in tests/test_reconnections.py, standalone, plain TCP and TLS. All count connection errors from the attempting reconnection / Maximum reconnection attempts lines: attempt_reconnect() logs exactly one of them for each error it counts, on every transport. The Connection error: line is TLS connection error: over TLS and is missing when errno is 0.

  • test_connection_errors_reported_when_server_never_reachable: refused port, -c 2, --max-reconnect-attempts=2.
    • Checks logged ≤ Connection Errors ≤ 2 × (1 + 2).
    • Sets/Gets/Waits/Totals Ops/sec and KB/sec are 0.0.
    • No null or NaN appears outside configuration, and every Time-Serie second is ≤ 31.
  • test_ctrl_c_reported_when_server_never_reachable: SIGINT after 3 s.
    • The message reports at least 2.0 s, and Interrupted is "true".
    • Checks logged ≤ errors ≤ logged + 2.
    • Rates are 0, there is no null/NaN, and the Time-Serie is in range.
  • test_connection_errors_reported_when_only_a_later_client_connects (mixed case).
    • Setup: client 0 is refused on every attempt. Client 1, added by the staircase ramp at 3 s, connects through a one-connection proxy. The proxy port is bound but does not listen until memtier logs client 0's second failure.
    • --test-time=8, so client 0's last dial at 6 s happens before the deadline, also with End a --test-time run at its deadline while reconnecting to a dead server #666's reconnect deadline.
    • Asserts: logged (≥ 2) ≤ errors ≤ 1 + 2, Count > 0, 5000 ≤ Total duration ≤ 12000 ms (the run ends when that dial fails at about 6 s), Ops/sec consistent with Count/duration, no null/NaN, Time-Serie seconds ≤ 9.
  • The refused port stays bound for the whole run, so no other process can take it.

Measured locally on RLTest's own standalone env (port 14110), plain and --tls. Columns are the three tests in the order above:

binary never reachable Ctrl+C mixed
origin/master f308ed6 FAIL (0 >= 5) FAIL (after 0.0, 0 >= 6) FAIL (0 >= 3)
16b6ada (first head) FAIL (None == 0.0 rates, nulls) FAIL (same) PASS
M3: errors to m_totals PASS PASS FAIL (0 >= 3)
M4: counter advanced for every client PASS PASS FAIL (3002 >= 5000, duration halved)
M5: no early return for never-started runs FAIL (10 <= 6) FAIL (12 <= 8) FAIL (6 <= 3, epoch-keyed Time-Serie)
this PR f96e9cf PASS 5/5 TCP, 5/5 TLS PASS 5/5, 5/5 PASS 5/5, 5/5
this PR merged with #666 (692186c) PASS 5/5 TCP, 5/5 TLS PASS 5/5, 5/5 PASS 5/5, 5/5

Each mutant was run on TCP and TLS, with the same result on both. The previous mixed-test schedule (--test-time=5) fails on the #666 merge with 2 >= 3. The 0905dcf tests fail over TLS with 0 >= 3 at line 718, as in CI. The rest of tests/test_reconnections.py passes, except test_reconnect_on_connection_kill, which fails the same way on master under existing-env (ExistsRedisEnv has no dbDirPath).

Affected versions

2.3.0 through 2.5.2 (latest) and master.

Fixes #702

🤖 Generated with Claude Code


Note

Medium Risk
Changes how run stats are merged and summarized on failure/interrupt paths; behavior is narrow but affects final JSON/text reporting when connections never succeed.

Overview
Fixes #702: final reports and JSON no longer drop connection errors and interrupted state from clients that never established a connection, while still excluding their zero timestamps from merged run duration.

client_group::merge_run_stats() now merges every client; only clients that has_started() advance the factorial-averaging iteration counter. run_stats::merge() treats never-started peers separately: it folds their connection errors into m_cur_stats, propagates m_interrupted, and skips timestamp/per-second bucket merge (avoids epoch-keyed time series and corrupted durations).

On Ctrl+C, the “after N secs” line uses wall time since benchmark_start_time instead of averaged client durations (which stay 0 until connect). totals_cmd::summarize() reports 0 ops/KB rates when duration is 0 so text/JSON avoid NaN/null when no client ever connected.

Adds three reconnection integration tests (unreachable server, SIGINT before connect, mixed staircase + refused client).

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

@github-actions

github-actions Bot commented Oct 16, 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 merge path and the tests, and I only have a couple of small questions.

The fix looks right to me. All client groups merge into the same stats in run_benchmark(), so the errors put in the target's m_cur_stats are not lost at a second merge step, and summarize() already picks up m_cur_stats. The zero-duration guard in totals_cmd::summarize() covers every rate the function sets, so nothing is left stale. The tests are shown failing on master and passing with the fix, on TCP and TLS, which is the same bar PR#364 met in this repo. Good to see.

  • In run_stats::merge(), the early return for a run that never started means anything such a run might record later is silently dropped, unless whoever adds it remembers this branch. I guess that's fine today, since only connection errors and the interrupted flag can happen before the first connect. Would a short assert or comment there help, e.g. that m_unanswered_requests and the histograms are expected to be empty? Not blocking.
  • The Ctrl+C "after N secs" value now counts the --clients-start ramp and connection setup. That's only stderr text, not JSON, so I don't think it's a compat concern. Just checking: do any of the CI scripts or tests parse that number and assume it's the averaged client duration?

The core change looks solid to me. The two points above are questions, not blockers.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Answers to each point, as of 0905dcf:

  1. Errors in m_cur_stats, not m_totals. Nothing reads the merged connection-error total before print(). --run-count picks BEST/WORST by get_total_ops() and the duration only. aggregate_average() calls summarize() on every run, which includes m_cur_stats. The live, statsd and Prometheus paths read the per-client stats. merge() now has a comment saying the totals are rebuilt by summarize() in print() and aggregate_average(), so the errors go to the current second.
    This point also turned up a regression: when no client starts, print() now runs summarize() with a zero duration. totals_cmd::summarize() then printed -nan and wrote null for the per-command Ops/sec and KB/sec, where master printed 0. That is fixed: rates are 0 for a zero duration, matching the existing hits/misses and connection-error guards. The dead-server tests now assert 0 rates and no null outside configuration, and they fail at 16b6ada.
  2. Mixed case. Added test_connection_errors_reported_when_only_a_later_client_connects. Client 0 is always refused. Client 1, added by the staircase ramp, connects through a one-connection proxy. The test checks the error count and the merged run time. It fails on master, with errors sent to m_totals (mutant), and with the averaging counter advanced for every client (mutant: duration halved). As you say, those errors are in Totals but not in any per-second entry. There is no start time to place them, and the per-second entries have no connection-error field. The PR body says this.
  3. Exact equality. Changed to >=. An error with errno 0 is counted but not logged, so the count can be higher than the number of log lines.
  4. _closed_port() race. Agreed. The tests now keep the socket bound and not listening for the whole run. Connects are refused, and no other process can take the port.

fcostaoliveira and others added 3 commits October 16, 2026 14:10
When no connection ever succeeds, no client sets a start time, and
client_group::merge_run_stats() skipped every such client so that its
zeroed start time would not be averaged into the merged timestamps. The
connection errors and the interrupted flag those clients recorded were
dropped with them: the final report and JSON said "Connection Errors: 0"
although the live line counted every failed connect, and a Ctrl+C left
"Interrupted": "false". The Ctrl+C message averaged the client durations,
which are 0 for clients that never started, and printed "after 0.0 secs".

Merge every client. run_stats::merge() now takes only the connection
error count, the interrupted flag and the unanswered requests of a run
that never started, adding the errors to the current second that
summarize() already includes; its timestamps and per-second buckets
(keyed from the epoch, since the start time is zero) are still left out,
and it does not advance the averaging counter. The Ctrl+C message uses
the wall time since the run started.

Fixes #702

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the connection errors of never-started clients in the current
second, print() now calls summarize() for a run whose merged duration is
0. totals_cmd::summarize() divided by that duration, so the Sets, Gets
and Waits Ops/sec and KB/sec printed -nan and were null in the JSON (also
in BEST/WORST with --run-count), where master printed 0. Return 0 rates
for a zero duration, like the hits/misses and connection error rates.

Drop the unanswered-requests merge of never-started clients: they sent
no requests. Say in merge() why the errors go to m_cur_stats and not to
m_totals.

Tests: assert 0 rates and no null outside "configuration" in the JSON
of the dead-server runs; compare the connection error count with >=,
since an error with errno 0 is counted but not logged; keep the refused
port bound for the whole run instead of closing it first; and add a mixed
run where client 0 is always refused and client 1 connects through a
one-connection proxy, which checks the error count and that the merged
run time is not halved by client 0's zero timestamps.

Fixes #702

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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

Copy link
Copy Markdown
Collaborator Author

Re the automated review posted at 13:14:

  • The early return in run_stats::merge() for a never-started run: the comment there lists what such a run can carry: its connection errors and its interrupted flag. That is everything that can happen before the first connect, since requests are only sent once a client has started. Don't roll per-second stats before a client's start time is set (#708) #714 (now merged) also keeps such a client's per-second stats from rolling before it starts, so its histograms and buckets stay empty. A future addition recorded before the start would be caught by the tests here: they assert the totals and that no null, NaN or epoch-sized Time-Serie key appears.
  • Who parses "after N secs": within the repo, only this PR's own test_ctrl_c_reported_when_server_never_reachable does (tests/test_reconnections.py), and it expects the wall-clock value. No CI script reads it. The other interrupt tests only match the message text. The PR description notes that the value now includes connection setup and the --clients-start ramp.

@fcostaoliveira
fcostaoliveira merged commit f50bd03 into master Oct 16, 2026
129 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
With more than one thread, run_benchmark() calls merge_run_stats() once
per thread, all into the same run_stats. merge_run_stats() started its
averaging weight at 1 on every call, and run_stats::merge() computes the
running mean as (avg * (w - 1) + new) / w, so the first client of each
thread replaced the average built so far. The merged Start time, Finish
time and Total duration, and every Ops/sec and KB/sec derived from them,
came only from the last thread's clients.

Let the caller own the counter: run_benchmark() passes the same one to
every thread's merge_run_stats(), so the weight keeps growing across
threads. Clients that never started still do not advance it (#707).

The test runs four rate-limited single-client threads and disconnects
one client once, so it ends about 1 s after the others. The merged
duration must be the mean of the four (about 3.25 s), not the last
thread's 3 s or 4 s.

Fixes #709

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
The merged run's Start time and Finish time are a running mean over
every client that started, but client_group::merge_run_stats() reset
its averaging counter for each thread. With more than one thread the
last thread's clients outweighed all the others, so the merged Start
and Finish time, Total duration and every rate (Ops/sec, KB/sec,
hits/misses/aborts per second) came mostly from the last thread. A
delayed client in another thread was ignored.

The caller now owns a single counter that spans all threads; clients
that never started still do not advance it (#707's rule).

timeval_factorial_average() also overflowed int64 when the weight times
the epoch-microsecond timestamp got large (about 5100 clients), which
gave negative Start/Finish times in the JSON. It now computes
a + floor((b - a) / w), which cannot overflow and equals the old
result whenever the old one did not overflow.

The .hgrm log written with --hdr-file-prefix uses the same merged
times, so its StartTime header and interval timestamps change the same
way for multi-thread runs.

Tests: a unit test for the merge arithmetic (fails on master with
negative times) and an RLTest that delays one client in the first
thread and checks the merged duration includes it (master fails 10/10).

Fixes #709

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.

"Connection Errors" is 0 in the final report and JSON when no connection ever succeeds, although the live line counts them

1 participant