Skip to content

Don't roll per-second stats before a client's start time is set (#708) - #714

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix-708-late-start-stats
Oct 16, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix-708-late-start-stats

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --reconnect-on-error, when a client's first connect fails and a later reconnect succeeds (for example, redis started a couple of seconds after memtier), a --test-time run stops as soon as the connection is up. It sends 0 requests and still exits 0. The JSON has Count 0 and "Total duration": 0, and its Time-Serie keys are epoch seconds (['0', '1792151574', ...]). With --requests the run completes, but its time series is keyed the same way.

Root cause

shard_connection::attempt_reconnect() calls run_stats::update_connection_error() before the client has started. That function calls roll_cur_stats(), which computes sec = ts_diff(m_start_time, *ts) / 1000000 while m_start_time is still {0,0}. So m_cur_stats is reset to a second near 1.79e9. set_start_time() leaves m_cur_stats alone, and later rolls never go back. Once the client connects, get_duration() (which is m_cur_stats.m_second) is already >= test_time, and client::finished() ends it.

Fix

roll_cur_stats() returns early while !has_started(). Errors recorded before the start are still counted in m_totals and in m_cur_stats second 0, which the first real second then continues. This is a single-line behaviour change in run_stats.cpp.

Test evidence

New tests/test_reconnect_late_server_start.py (standalone only) has two cases. Each starts memtier (-t 1 -c 2 --reconnect-on-error --connection-stage-timeout=0) against an empty port and starts redis on that port 2 s later:

  • test_server_started_after_memtier uses --test-time=5.
  • test_server_started_after_memtier_requests uses --requests=1000.

Both assert:

  • exit code 0;
  • Connection Errors == the number of logged (TLS c|C)onnection error: lines, and at least 2 per client;
  • Count > 0;
  • Count == the server's cmdstat_get + cmdstat_set calls;
  • small Time-Serie keys;
  • for --test-time: Total duration in [4 s, 7 s];
  • for --requests: Count == 2000.

If another process took the port before redis started on it, the test fails with a clear message instead of timing out.

binary result
origin/master 2/2 tests fail, every run (5 runs). Count 0, duration 0, keys [0, 1792153397, ...].
mutant M4: m_cur_stats.reset(0) in the guard (drops the pre-start errors) 2/2 fail (2 runs): Connection Errors 2 == 6
mutant M1b: no guard, set_start_time() clears m_stats and resets m_cur_stats 2/2 fail: Connection Errors 0 == 6
this branch 18/18 pass (9 runs x 2 tests)

Manual repro, master vs fix:

master: Count 0      ConnErr 6  Duration 0     TS keys ['0','1792151574','1792151575','1792151576']
fix:    Count 248211 ConnErr 6  Duration 5000  TS keys ['0','1','2','3','4','5']
--requests 1000, master: Count 2000  TS keys ['0','1792151641','1792151642','1792151643']
--requests 1000, fix:    Count 2000  TS keys ['0']

These existing tests pass locally on this branch: test_reconnections.py test_reconnect_unlimited_no_spurious_thread_restart, test_connection_kill_with_reconnect_is_not_rerun and test_dropped_in_flight_requests_do_not_stall_client.

Fixes #708

Affected versions

2.3.0 through 2.5.2 (latest) and master. On master the result is a 0-op run. In 2.5.x the silent worker-thread restart hides it: the run takes extra wall time, and the JSON reports Connection Errors 0.

🤖 Generated with Claude Code


Note

Low Risk
Narrow guard in stats rolling for the pre-start reconnect path; regression tests cover late server startup with --reconnect-on-error.

Overview
Fixes #708: with --reconnect-on-error, pre-start connection errors no longer advance per-second stats using an unset m_start_time, which used to jump m_cur_stats.m_second to epoch-scale values and make --test-time runs exit immediately with 0 requests and bogus JSON Time-Serie keys.

run_stats::roll_cur_stats() now returns early while !has_started(), so errors before the first successful connect stay in second 0 and totals until the real start; duration and time-series indexing behave normally after connect.

Adds tests/test_reconnect_late_server_start.py: starts memtier against a closed port, brings up redis after 2s, and checks exit code, connection errors, request counts vs server commandstats, runtime bounds for --test-time, and sane time-series keys for --test-time and --requests.

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

fcostaoliveira and others added 2 commits October 16, 2026 13:20
With --reconnect-on-error, a client whose first connect fails records a
connection error through run_stats::update_connection_error() before
set_start_time() has run. roll_cur_stats() then measured the current
second from a zero m_start_time, so m_cur_stats moved to an epoch-sized
second (~1.79e9). set_start_time() does not reset it and later rolls
never go back, so get_duration() reported the run over as soon as the
client connected: a --test-time run sent 0 requests and exited 0, and the
JSON time series was keyed by epoch seconds (also with --requests).

Make roll_cur_stats() a no-op until the run has started. The early
connection errors are still counted in m_totals and in second 0, which
the run's first second then continues.

Fixes #708

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assert that Connection Errors equals the logged connection errors (at
least two per client), so dropping the errors recorded before the start
fails the test, and that the server saw exactly Count GET/SET calls.
Cover --requests too. Fail with a clear message if another process took
the port before redis was started on it.

Refs #708

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-708-late-start-stats branch from b7e8234 to 7dda96a Compare October 16, 2026 12:27
@github-actions

Copy link
Copy Markdown

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

Thanks for this. The root-cause writeup made the one-liner easy to follow. Returning early in roll_cur_stats() while !has_started() looks right to me. Errors from before the start still go to m_totals and to bucket 0, set_start_time() doesn't touch m_cur_stats, so the first real second just continues from 0, and nothing on the --requests path depends on the old epoch-sized second. I also like that the test fails on master and passes here, and that it covers both --test-time and --requests. This is the same bar tests/test_json_output_integrity.py (PR#364) set.

A couple of small questions:

  • In _check_common, assertEqual(totals["Connection Errors"], logged) ties the counter to the stderr text. In handle_event() the Connection error: line only prints if (!ssl_error && errno), but attempt_reconnect() always calls update_connection_error(). If errno has already been cleared when libevent runs the deferred BEV_EVENT_ERROR callback (an immediate loopback ECONNREFUSED can take that path), the error gets counted with no log line and this check flakes. Maybe the macOS cells are where that shows up first. I may be wrong about how libevent handles this, but would assertGreaterEqual(totals["Connection Errors"], 2 * CLIENTS) still catch the regression you care about (pre-start errors being dropped) without depending on the log format?
  • Nitpicking: roll_cur_stats() runs on every op, so this adds an acquire load of m_started to the per-request path. On x86 it costs nothing and I doubt it's measurable anywhere. I'm only mentioning it because a plain read would do here, since the flag is written by the same thread.

The core fix looks good to me. I'd just like to hear back on the test assertion before this merges.

The "Connection error:" line is only printed when errno is set, while
attempt_reconnect() always counts the error, so an exact match with the
log could flake. Assert at least two errors per client instead, which
still fails if the errors recorded before the start are dropped.

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

Copy link
Copy Markdown
Collaborator Author

Re the automated review:

  • Test assertion: agreed, changed in the new head. _check_common now asserts Connection Errors >= 2 * CLIENTS and no longer compares it with the logged Connection error: lines. As you say, that line is only printed when errno is set, while attempt_reconnect() always counts the error. The new bound still catches the regressions that matter: dropping the errors recorded before the start leaves 2 (m_cur_stats.reset(0) in the guard) or 0 (resetting the stats in set_start_time()). The test passes locally with --env oss.
  • Acquire load on the per-op path: fair point. It is free on x86 and a single ldar on arm64. has_started() is the existing accessor with acquire semantics, because another thread can read it. A relaxed variant just for this call would add an API for no measurable gain, so it stays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant