Skip to content

Exit 2 when any --run-count run hits --connection-stage-timeout - #752

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix/739-run-count-exit-code
Oct 16, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix/739-run-count-exit-code

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --run-count N, if a run other than the last one is aborted by --connection-stage-timeout, memtier exits 0 (or 1 if another run failed). The aborted run is still part of BEST/WORST/AGGREGATED, which the JSON marks "Interrupted": "true". Exit 2 was only returned when the last run aborted.

Root cause

main() read g_connection_stage_aborted once, after the run loop. connection_stage_supervisor_reset() clears that flag at the start of every run, so that a run-1 abort does not stay latched into run 2 and suppress its worker-failure report in cg_thread_start(). The flag therefore described only the last run, while g_worker_failed (exit 1) is never reset and covers every run.

Fix

Keep the per-run flag for the in-run logic. Add g_any_run_connection_stage_aborted, set by the main thread next to it in run_benchmark() and never reset, and use it for the exit code.

The two exit(1) paths that can end a later run before main() returns now exit through failure_exit_code(), so they return 2 after an earlier abort. These are a thread that fails to prepare (for example a failed --unix-socket connect) and a failed --reconnect-interval reconnect. The latch is atomic because a worker thread reads it.

The existing precedence is kept and now also applies across runs: 2 wins over 1, whether the abort and the worker failure (or output write error) happen in the same run or in different runs. This matches test_connection_stage_abort_keeps_rc_2_on_write_error. The remaining runs still start after an abort, as before, and the aggregated output already flags the aborted run as Interrupted. README documents the exit status of a --run-count series in the "Exit status" list, including Ctrl+C after an aborted run and the fact that the aborted run still counts in AGGREGATED. --help and the man page say that an abort in any run exits 2.

Tests

New tests/test_run_count_exit_code.py. A scripted RESP peer decides per connection what happens. With -t 1 -c 1 and no --reconnect-on-error, connection n is run n. A run either completes, rejects AUTH (the run aborts), or is closed after 9 replies (a worker failure, exit 1). The test checks the exit code and the abort count for each plan:

plan master this PR
abort, ok, ok 0 (FAIL) 2
ok, abort, ok 0 (FAIL) 2
abort, drop 1 (FAIL) 2
ok, ok, abort 2 2
drop, abort 2 2
drop, ok 1 1
ok, ok 0 0
abort, refuse (unix socket) 1 (FAIL) 2
ok, refuse (unix socket) 1 1

"refuse" removes the --unix-socket path after the previous connection closes, so that run's thread prepare fails.

The first plan also checks that AGGREGATED and WORST are Interrupted: true and BEST is false.

Measured at the current head: 6 of 6 full runs of the file passed (9/9 tests each). On master (a7881e0), the same 4 tests failed. At the first commit 04ed3da, only the unix-socket abort test failed (rc 1). The test skips cluster and TLS cells because it only uses its own scripted server. These suites also pass with the fix: test_cluster_slots_bad_reply.py (16), test_cli_validation_run_count.py (5), test_reconnect_late_server_start.py (2), test_reconnections.py (17), and tests_oss_simple_flow.py:test_connection_stage_abort_keeps_rc_2_on_write_error.

I also ran the repro from the issue (-OOM until the preloaded keys expire) with --run-count 3 on master a7881e0. Run 1 aborted and the exit code was 0.

The unit tests that link shard_connection.cpp (cluster_hslot, cluster_transaction, retry_drain, run_stats_merge, socket_options) get an int failure_exit_code() { return 1; } stub next to their other main-owned stubs. make check passes locally (10/10).

Merge note: open PR #666 adds tests/unit/reconnect_siblings_test.cpp, which has the same stubs. Whichever of #666 and this PR merges second must add the failure_exit_code() stub there as well, or make check fails at link time.

Fixes #739

Release note

  • Exit status change with --run-count: if any run is aborted by --connection-stage-timeout, memtier now exits 2, even when a later run completed or failed. Before, only the last run counted, so such a series could exit 0 or 1. A series mixing an abort with a worker failure or output write error (which used to exit 1) now exits 2, the same precedence a single run already had. Scripts that treat 1 and 2 differently will see the new code for that mix.

Affected versions

2.4.0 through 2.5.2 (latest) and master. Introduced by 2e8869e "fix(connection-stage): bound retry loop with --connection-stage-timeout (Refs #426 phase 1) (#431)", which added the flag, its reset per run and exit 2. The first tag that contains it is 2.4.0. Reproduced on master a7881e0.

🤖 Generated with Claude Code

fcostaoliveira and others added 2 commits October 16, 2026 23:31
With --run-count > 1, a connection-stage abort in a run other than the
last one was lost: the series exited 0 (or 1 if another run failed), even
though the aborted run is part of BEST/WORST/AGGREGATED and marks them
Interrupted. main() read g_connection_stage_aborted once after the run
loop, and connection_stage_supervisor_reset() clears it at the start of
every run so that a run-1 abort does not suppress run 2's worker-failure
report. So the flag only described the last run, while g_worker_failed
(exit 1) is sticky across runs.

Keep the per-run flag for the in-run logic and latch a separate
g_any_run_connection_stage_aborted, set by the main thread next to it and
never reset, which main() uses for the exit code. The existing precedence
is kept: 2 wins over 1, now also when the abort and the failure happen in
different runs. The remaining runs still start after an abort, as before.

The new test drives a scripted RESP peer that decides per connection (one
per run with -t 1 -c 1) whether a run completes, aborts on AUTH, or fails
mid-run, and checks the exit code of each mix.

Fixes #739

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two failures end memtier with exit(1) before main() returns: a thread
that fails to prepare (for example a --unix-socket connect that fails)
and a failed --reconnect-interval reconnect. In a later --run-count run
they bypassed the any-run-aborted latch, so a series whose run 1 was
aborted could still exit 1. Both now exit through failure_exit_code(),
which returns 2 when an earlier run was aborted and 1 otherwise. The
latch is now atomic, since a worker thread reads it.

The test covers the prepare path with a scripted --unix-socket peer
that removes its socket after run 1, and asserts that the drop runs
really failed. The test skips cluster and TLS cells, since it only uses
its own peer. README lists the --run-count rule with the other exit
statuses, covers Ctrl+C after an aborted run, and notes that the aborted
run still counts in AGGREGATED. --help and the man page say that an
abort in any run exits 2.

Refs #739

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 diff and the run loop in main(), and the fix looks right to me. Keeping the flag that resets each run for the in-run logic in cg_thread_start(), and adding a separate flag that is never reset for the exit code, seems like the smallest change that keeps #431's reasons for the per-run reset. I also checked the other exit(1) calls in memtier_benchmark.cpp, obj_gen.cpp and JSON_handler.cpp. As far as I can tell, they all happen during startup or option validation, before the first run starts. So the two you moved to failure_exit_code() are the only ones that can end a later run, unless I missed one.

A few small things:

  • Just checking: a series like abort, then a failed run, used to exit 1 and now exits 2. I think that's the right call, since it's the precedence a single run already has, and README now documents it. Still, any CI script that treats 1 and 2 differently will see a different code for that mix. It may be worth a line in the release notes, not only in README.
  • Nitpicking: this adds the same failure_exit_code() stub to five unit tests, and as you noted, End a --test-time run at its deadline while reconnecting to a dead server #666 will need a sixth. I know socket_options_test.cpp explains why it can't use tests/fuzz/fuzz_stubs.cpp. Did you consider a small shared stub file for the functions owned by memtier_benchmark.cpp, for the tests that link shard_connection.cpp? Not a blocker, it just keeps the next main-owned helper from needing six edits.
  • Nitpicking: in the test, the "refuse" plan unlinks the socket in connection_closed() based on self.connections. That relies on run n's connection closing before run n+1 connects, which holds with -t 1 -c 1 and the 1 s settle pause. A short comment there saying the ordering depends on that would help the next person who changes the thread or client counts.

The core fix and the tests look solid to me. Only the release-note question seems worth answering before merge.

….cpp

shard_connection.cpp now calls failure_exit_code() from
memtier_benchmark.cpp, which the unit tests do not link, so make check
failed at link time. Add the stub next to the other main-owned stubs.

Refs #739

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

Copy link
Copy Markdown
Collaborator Author

Thanks. Point by point:

  • Release note: added a "Release note" section to the description. With --run-count, any run aborted by --connection-stage-timeout now makes memtier exit 2, and a series mixing an abort with a worker failure or output write error (previously 1) now exits 2. That is the precedence a single run already had. It is called out for scripts that treat 1 and 2 differently.
  • Shared stub file for the main-owned hooks: agreed it would save edits. I'd rather do that as its own small follow-up that moves all the existing per-test stubs at once, not only failure_exit_code(). That keeps this PR to the exit-code fix.
  • Unlink ordering in the "refuse" plan: right, it relies on -t 1 -c 1 and the settle pause, so run n's connection closes before run n+1 connects. I'll add the comment with the follow-up above.

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.

--run-count: exit code 2 (--connection-stage-timeout abort) is lost unless the last run aborted

1 participant