Skip to content

state_exec: a run reads stop()'s flag with acquire - #1073

Merged
transfix merged 1 commit into
masterfrom
fix/scheduler-stop-acquire
Nov 9, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/scheduler-stop-acquire

Conversation

@transfix

@transfix transfix commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

sanitizers / clang-tsan fails now and then in state_exec_hazards_test, Hazards.ASchedulerStoppedFromAnotherThreadMayGoWhenItsRunReturns:

WARNING: ThreadSanitizer: data race
  Write of size 8 by main thread:
    #0 operator delete(void*)
    #1 cvc::state_exec::scheduler::~scheduler() scheduler.cpp:104
    ...
    #5 Hazards_ASchedulerStoppedFromAnotherThreadMayGoWhenItsRunReturns_Test::TestBody() state_exec_hazards_test.cpp:571
  Previous atomic write of size 1 by thread T119:
    #1 std::atomic<bool>::store(bool, std::memory_order)
    #2 cvc::state_exec::scheduler::stop() scheduler.cpp:899

It hit master's own run at 04b72a3 (job 128843093768) and #1068, a test-only change to ariadne_media_test (job 128836230849). Most runs pass.

Cause

stop() stores stop_requested_ with memory_order_release. The run loops read it with memory_order_relaxed: run_steps(), the wait loop in idle_wait(), and async_scheduler::run_loop(). A relaxed read synchronizes with nothing, so nothing orders stop()'s store before what the run's owner does after run() returns. The test, like any owner, destroys the scheduler at once, and that delete races the store. The header comment said "No data rides on them", but the scheduler's lifetime does. (#1014 already made stop() take its own reference to the idle wakeup for this same destroy-on-return case. This closes the remaining gap, the flag itself.)

Fix

Read the flag with memory_order_acquire in all three places, so a run that ends on stop() synchronizes with its release store. On x86 an acquire load is the same plain mov. Elsewhere it is one ordered load per loop iteration. The scheduler.h and async_scheduler.h comments and docs/STATE_EXEC_THREADED.md's stop() paragraph now say why.

A standalone atomic_thread_fence(acquire) after the loop would also be correct, but TSan doesn't model fences, so the loads carry the ordering instead.

Testing

  • The clang-tsan leg on this PR is the real check. TSan catches the race only on some runs, so one green run isn't proof by itself. The fix follows from the memory model: a release store read by an acquire load synchronizes with it.
  • Windows (MSVC 14.44 under vcvars64, static Release, cvcpkg deps): state_exec_hazards_test 12/12, state_exec_scheduler_test 199/199 and state_exec_async_test 106/106 pass with --gtest_filter=-*Stress* (as the sanitizer legs run them). Hazards.ASchedulerStoppedFromAnotherThreadMayGoWhenItsRunReturns also passes 20 repeats of 2000 stop-then-destroy runs each. That run has no TSan, so it checks for regressions, not for the race.

stop() stores stop_requested_ with release, but run_steps(), idle_wait()
and the async run_loop() read it relaxed, so nothing ordered stop()'s
store before what the run's owner does once run() returns. An owner may
destroy the scheduler at once (Hazards.ASchedulerStoppedFromAnotherThread-
MayGoWhenItsRunReturns does), and that delete then raced the store:
clang-tsan reported it on master (04b72a3) and on unrelated PRs, as a
data race between ~scheduler()'s operator delete and stop()'s
atomic<bool>::store.

Read the flag with acquire in all three places. On x86 that is the same
plain load; elsewhere it is one ordered load per loop iteration.
@transfix
transfix merged commit 104aafe into master Nov 9, 2026
26 of 30 checks passed
@transfix
transfix deleted the fix/scheduler-stop-acquire branch November 9, 2026 01:43
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