Repository navigation
Stamp interrupted clients' end time after joining the workers (Ctrl+C data race) - #730
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. I went through the diff and the interrupt/join path in A few questions, none blocking:
On compat: the JSON Totals/per-second counts can go up slightly because late replies are now counted. That's disclosed, it brings JSON in line with HDR/Prometheus, and the keys and The fix looks solid to me. Given how central this shutdown ordering is, it may still be worth a second look from whoever owns the Prometheus run-end drain before it merges. |
dfec512 to
7a6a245
Compare
|
Thanks for the review. Taking your points in order (pushed as 7a6a245, rebased on master 0fc29f4):
Also in this push:
Known corner, not changed: if a worker throws after an interrupt, it is marked failed and finalizes its own clients with the current time, not the interrupt time. That path runs on the worker, so there is no race. Only the end time can be a few ms later than the interrupt. |
client_group::interrupt() runs on the main thread. It asked the worker loop to stop (event_base_loopbreak()/loopexit(), which take effect only after the callback that is running) and then immediately called finalize_all_clients(), i.e. run_stats::set_end_time() on every client. That folds the current second (summarize_current_second() writes the shared quantile scratch vector) and appends it to m_stats, while the worker can be in handle_response() -> roll_cur_stats() doing the same on the same objects, with no lock. A data race on Ctrl+C, SIGTERM and the connection-stage abort; a double free under ASan. The TSan suppression for run_stats::set_end_time hid it in CI. interrupt() now only records the interrupt time, marks the clients interrupted and breaks the loop. run_benchmark() finalizes the clients that have not ended with that time right after pthread_join(), before merging their stats. The duration and JSON stay as before: the end time is still the interrupt time, and "Interrupted" is still set. With the fold now on the joined thread, the keep_live_totals special case for the Prometheus exporter is no longer needed and is removed. Drop the race:run_stats::set_end_time suppression and add test_interrupt_does_not_race_worker_stats, which fails under TSan on master (TSan report, exit 66) and passes with the fix. Fixes #710 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The regression test for #710 only catches the race under TSan, which reports it on most interrupts but not all, and the TSan CI cells are continue-on-error. client_group now records the worker thread in run() and is marked joined after pthread_join(); finalize_all_clients() aborts with "BUG: finalize_all_clients() off the worker thread before join" when called from any other thread before the join. On the old interrupt() this aborts on every SIGINT, SIGTERM and connection-stage abort, so the existing interrupt tests fail in every CI cell. Also make run_stats::m_interrupted atomic: the connection-stage supervisor sets it from the main thread while a worker whose loop has already ended reads it in client_group::run() (seen once under TSan). Refs #710 Co-Authored-By: Claude Opus 5.5 <[email protected]>
check_stats_owner() reads m_worker_tid from the main thread while the worker writes it in client_group::run(). Publish it with a release store of m_worker_tid_set and read it after an acquire load. Refs #710 Co-Authored-By: Claude Opus 5.5 <[email protected]>
7a6a245 to
bae456e
Compare
|
Answers to the second automated review (pushed as bae456e, rebased on master 193f6a8, which includes #724):
Re-run after the rebase: |
|
Thanks. Answering the 20:20 version of the review:
|
Problem
On Ctrl+C, SIGTERM, or a connection-stage abort, the main thread finalized every client's
run_statswhile the worker threads were still recording into them. That is a data race onstd::list/std::vectormembers, and a double free under ASan (#710).Root cause
client_group::interrupt()runs on the main thread. It callsevent_base_loopbreak()+event_base_loopexit({0,0}), and both take effect only after the callback that is currently running. It then calledfinalize_all_clients(true)right away, which meansrun_stats::set_end_time()→summarize_current_second()(writesm_cur_stats.*.summarized_quantile_valuesand the sharedm_quantile_valuesscratch) →m_stats.push_back(m_cur_stats). At the same moment the worker can be inhandle_response()→update_*_op()→roll_cur_stats(), which runs the same summarize/push_back/reset on the same objects with no lock. The plainbool m_end_setwas raced as well.CI never caught this because
tsan_suppressions.txthadrace:run_stats::set_end_time, and that matches the main-thread stack of exactly this race.Fix
interrupt()now only records the interrupt time (once), marks the clients interrupted and breaks the loop. It no longer touchesrun_stats.run_benchmark()calls the newclient_group::finalize_interrupted_clients()right after eachpthread_join()and beforemerge_run_stats(). It ends the clients that are still open, using the recorded interrupt time (client::set_end_time_interrupted(), clamped to the client's start time). By then the worker has exited, so nothing races."Interrupted": "true"is still set, and the partial-run and series-cut behaviour from Stop the --run-count series on Ctrl+C #698 / Handle SIGTERM like Ctrl+C so --json-out-file stays valid #706 is unchanged.keep_live_totalsspecial case inrun_stats::set_end_time()/summarize_current_second()existed only because the fold ran concurrently with the worker. It is removed. The post-join fold moves the live latency histogram into the exporter's pending histogram under the mutex as usual, and the run-end drain still picks up anything left over.race:run_stats::set_end_timeTSan suppression is removed.client_grouprecords its worker thread inrun(), andcg_thread::join()marks it joined afterpthread_join().finalize_all_clients()aborts withBUG: finalize_all_clients() off the worker thread before joinwhen it is called from any other thread before the join. The check is always on, because the race itself only shows up under a sanitizer, and even then not on every run. I put the old finalize back intointerrupt()locally to check it. The guard then aborts on every SIGINT, SIGTERM and connection-stage abort: 5/5 oftest_interrupt_signal_handling,test_interrupt_stops_run_count,test_interrupt_does_not_race_worker_stats,test_sigterm_closes_jsonandtest_rejected_setup_at_startup_is_bounded_by_connection_stage_timeoutfail. With the fix there are no false positives: all oftests_oss_simple_flow.pypasses (48/48), plustest_cluster_slots_bad_reply.py,test_client_staircase.py, prometheus F27 andmake check. The TSan cells are continue-on-error, so this guard is what makes the regression blocking in the plain, ASan and UBSan cells.run_stats::m_interruptedis now atomic. The connection-stage supervisor callsinterrupt()(which runsset_interrupted()) on groups whose worker may already have left the loop on its own and be reading the flag inclient_group::run(). CI TSan reported this once.Behaviour change: late replies are now counted
Replies the worker handles between
interrupt()and the loop exiting now go into Totals and into the final per-second bucket. Before, they landed inm_cur_statsafter the main thread had already pushed it. That dropped them from the JSON per-second and totals, while the HDR histograms and Prometheus (through the run-end drain) still counted them. Now JSON, HDR and Prometheus agree. The duration is still the interrupt time, so ops/sec can rise by at most one callback's worth (pipeline × connections). If that last callback crosses a second boundary,roll_cur_stats()can push a bucket that starts after the interrupt time. The worker could already do that before this change, and the per-second series simply ends with that bucket.Test evidence
New
tests_oss_simple_flow.py::test_interrupt_does_not_race_worker_stats: 3 × (-t 4 -c 10 --pipeline 16 --test-time 30, SIGINT after 2 s). It asserts no sanitizer report, exit 0,Interrupted: true, a duration that matches the interrupt time, and a non-zero count. To get the TSan exit code it has to run under a TSan build. CI's TSan cells run this file.Local runs, TSan build (
--enable-thread-sanitizer,setarch -R,TSAN_OPTIONS=suppressions=tsan_suppressions.txtfrom this branch):Direct CLI with
timeout -s INT 2: master reports the race in about 60% of single interrupts (3/5 for-t 2 -c 5 --pipeline 16, 4/5 for-t 4 -c 50 --pipeline 64), with 25 reports per hit. Every one hasclient_group::interrupt()→finalize_all_clients()→run_stats::set_end_time()on the main thread againstrun_stats::update_set_op()/handle_response()on a worker. The fix gave 0 reports in every run.Also passing locally with the fix (as of bae456e, rebased on 193f6a8):
test_interrupt_signal_handling,test_interrupt_stops_run_count,test_interrupt_between_runs_stops_run_count,test_interrupt_last_run_marks_aggregate,test_sigterm_closes_json,test_sigterm_stops_run_count,test_sigterm_between_runs_stops_run_count),test_prometheus.py::test_F27_interrupt_run_end_counts_every_op,test_reconnections.py::test_ctrl_c_reported_when_server_never_reachable, and the connection-stage abort tests intest_cluster_slots_bad_reply.py.test_prometheus.py, andmake check(9/9).Overlap with open PRs
client::set_end_time_at()and declares it in client.h next toset_end_time_keep_live_totals(), which this PR replaces withset_end_time_interrupted(). The conflict is a small textual one in client.h only, and the functions are independent.abandon_unanswered_requests(unanswered, held)sits next toset_end_time_interrupted()in client.cpp, and the rebase kept both unchanged.Fixes #710
Affected versions
2.3.0 through 2.5.2 (latest), and master. The bug was introduced by aa9edb8 "Signal Handling with Graceful Shutdown with metrics" (#325), and
git tag --contains aa9edb8→ first tag 2.3.0. Reproduced here on master 427d721 under TSan.🤖 Generated with Claude Code
Note
Medium Risk
Changes shutdown ordering and stats finalization on interrupt paths; logic is localized but affects correctness of reported run windows and thread-safety of hot
run_statspaths.Overview
Ctrl+C / connection-stage abort no longer finalizes
run_statson the main thread while workers may still be inhandle_response()/roll_cur_stats(). That concurrentset_end_time()was a data race (and ASan double-free); TSan had been masked by a suppression onrun_stats::set_end_time.client_group::interrupt()now only captures interrupt wall time once, marks clients interrupted, and breaks the libevent loop—it does not touch stats. After eachpthread_join(),finalize_interrupted_clients()stamps still-open clients viaset_end_time_interrupted()(end time clamped to each client’s start). Reported duration and"Interrupted": truestay tied to the interrupt moment, not join time.The interrupt-only
keep_live_totalspath inrun_stats::set_end_time()/summarize_current_second()is removed; post-join folding and Prometheus run-end drain behave as on normal shutdown. A new OSS test hammers SIGINT under load and checks sanitizers, duration, and op counts.Reviewed by Cursor Bugbot for commit dfec512. Bugbot is set up for automated code reviews on this repo. Configure here.