Skip to content

Time --connection-stage-timeout in microseconds, not whole seconds - #860

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix/connection-stage-timeout-subsecond
Oct 26, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix/connection-stage-timeout-subsecond

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

--connection-stage-timeout=N can abort after just over N − 1 seconds.

The supervisor stamps the run start and the first setup failure with tv_sec only, and compares whole seconds. The main loop polls it once per second (sleep(1)). Suppose the run starts at 10.99 s and a poll lands at 12.01 s: the supervisor sees 12 − 10 = 2 seconds after 1.02 s, so a 2 s timeout trips.

This makes test_cluster_unowned_slots:test_unowned_keys_survive_repeated_redial (from #799) flaky. It has a 2 s budget against about 1.1 s of injected server delays and backoffs. It failed in #545's libevent-compat (legacy, plaintext, standalone) cell:

Connection dropped, attempting reconnection 2/3 in 0.50 seconds...
memtier_benchmark: aborting after 2 seconds of connection-stage failures (last error: no response from server during setup).
  Wall time:        1.010s

Locally it failed 3 times in 180 runs: 2 of 70 on #545, 1 of 40 on #832, and 0 of 70 on master. Every failure was this abort after about one second. Neither #545 nor #832 touches this code; the failure depends on where in the second the run starts.

Deterministic reproduction (from review): start memtier at set points late in a wall-clock second, against a server that accepts and never replies, with --connection-stage-timeout=2. Master aborted early in 4 of 25 runs, after 1.016 s, still saying "2 seconds". With this PR none did; the fastest abort was 2.008 s.

Fix

  • Stamp the run start and the failure streak in wall-clock microseconds, and compare them with the timeout in microseconds.
  • The comparison moves to connection_stage_clock.h (connection_stage_timed_out()), so it can be unit-tested.
  • The abort message still reports whole seconds.
  • With the once-per-second poll, an abort now lands between N and N + 1 seconds instead of between N − 1 and N.

Tests

  • New unit test tests/unit/connection_stage_clock_test (make check): a phase that started late in a second has not timed out 1.02 s later. It times out at exactly 2 s and reports whole elapsed seconds, has no start time before the first failure, and uses timestamps far from the epoch. A mutant that compares whole seconds fails it, and so does one that converts the timeout to microseconds in 32 bits (a 5000 s case).
  • Integration tests on libevent 2.1.12, ASan + UBSan. I ran the 17 test files that use --connection-stage-timeout (connection setup rejection, cluster slots, unowned slots, reconnects, retries, CLI validation and others). Results are the same with and without the fix: 236 pass, and the one failure is the same pre-existing test_memcache_meta_protocol:test_meta_unsupported_options_fail_before_connection on both.

🤖 Generated with Claude Code


Note

Low Risk
Localized change to connection-setup abort timing with dedicated unit tests; no auth or protocol behavior changes beyond more accurate timeout enforcement.

Overview
Fixes --connection-stage-timeout so it measures elapsed setup time in wall-clock microseconds instead of comparing whole tv_sec values, which could abort after ~N−1 seconds when a phase started late in a second (flaky cluster setup tests).

Adds connection_stage_timed_out() in connection_stage_clock.h and switches the connection-stage supervisor in memtier_benchmark.cpp to microsecond stamps (g_conn_stage_*_usec, conn_stage_now_usec()); user-facing abort text still reports whole elapsed seconds. connection_stage_clock_test is wired into make check to lock in boundary behavior (including large timeouts and year-2100 timestamps).

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

fcostaoliveira and others added 2 commits October 25, 2026 00:49
The connection-stage supervisor stamped the run start and the first
failure with tv_sec only and compared whole seconds. A run that started
late in a second could abort after just over N - 1 seconds of a
--connection-stage-timeout of N: the main loop polls once per second,
and a poll a few milliseconds past a second boundary saw two elapsed
seconds where barely one had passed. test_unowned_keys_survive_repeated_
redial (2 s budget, about 1.1 s of injected delays) fails this way about
once in 40 runs, aborting after 1.01 s.

Stamp both in microseconds and compare against the timeout in
microseconds. The comparison moves to connection_stage_clock.h, and a
unit test checks the boundary (1.02 s is not 2 s; exactly 2 s is). The
"aborting after N seconds" message still reports whole seconds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- .gitignore: /connection_stage_clock_test, like the other unit tests.
- The unit test checks a 5000 s timeout, so converting it to
  microseconds in 32 bits fails it.
- Fix the run-start variable name in the supervisor comment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit fb64304 into master Oct 26, 2026
58 of 59 checks passed
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.

1 participant