larson: fix two intermittent completion hangs#266
Open
hosking wants to merge 1 commit into
Open
Conversation
runthreads() spins on each slot's `finished` flag to detect worker completion, but two bugs can leave a slot's `finished` false forever, hanging the loop: 1. Data race. `stopflag` and `thread_data::finished` are shared between the main thread (sets stopflag, reads finished) and the worker/respawn threads (read stopflag, write finished) as plain `volatile int`. `volatile` provides neither atomicity nor inter-thread ordering, so these are unsynchronized data races (undefined behavior) that ThreadSanitizer flags. Make both `std::atomic<int>`. 2. Missing completion on early-return. A worker that starts after main has set `stopflag = TRUE` hits `if (stopflag) return 0;` and returns WITHOUT setting `finished = TRUE`, leaving the slot's `finished` at its initial 0 forever. Mark the slot finished on that early-return. Both are latent and timing-dependent: they surface intermittently when thread startup/scheduling is slow enough to deliver a just-created worker after stopflag is set (e.g. under an allocator whose per-thread setup delays a worker), while faster allocators miss the window by luck. Verified with ThreadSanitizer (data races eliminated) and by looping the repro (previously frequent hangs -> 0). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
runthreads()waits for the worker threads by spinning on each slot'sfinishedflag (while (!de_area[i].finished) sched_yield();). Two bugs can leave a slot'sfinishedfalse forever, solarson/larson-sizedoccasionally hang at shutdown instead of finishing.Both are latent, timing-dependent races: they surface when thread startup/scheduling is slow enough to widen the window (I hit them consistently under a GC-based allocator whose per-thread setup delays a worker past
stopflag; the system allocator misses the window by luck). They are allocator-independent bugs in the harness itself.1. Data race on
stopflag/finished. Both are shared between the main thread (setsstopflag, readsfinished) and the worker/respawn threads (readstopflag, writefinished) as plainvolatile int.volatilegives neither atomicity nor inter-thread ordering, so these are unsynchronized data races (UB). ThreadSanitizer flags both (on a standalone build with the system allocator — allocator-independent). Fix: make themstd::atomic<int>.2. Missing completion on the
stopflagearly-return. A worker that starts after main has setstopflag = TRUEhitsif (stopflag) return 0;and returns without settingfinished = TRUE, leaving that slot'sfinishedat its initial0forever → main spins on it. Fix: mark the slot finished before that early-return.Verification
stopflagandfinisheddata races are reported before the change and gone after.Two lines of behavioral change (
std::atomicdecls +finished = TRUEon early-return) plus#include <atomic>; no change to the benchmark's allocation behavior.🤖 Generated with Claude Code