Repository navigation
state_exec: a run reads stop()'s flag with acquire - #1073
Merged
Merged
Conversation
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.
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.
sanitizers / clang-tsanfails now and then instate_exec_hazards_test,Hazards.ASchedulerStoppedFromAnotherThreadMayGoWhenItsRunReturns: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()storesstop_requested_withmemory_order_release. The run loops read it withmemory_order_relaxed:run_steps(), the wait loop inidle_wait(), andasync_scheduler::run_loop(). A relaxed read synchronizes with nothing, so nothing ordersstop()'s store before what the run's owner does afterrun()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 madestop()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_acquirein all three places, so a run that ends onstop()synchronizes with its release store. On x86 an acquire load is the same plainmov. Elsewhere it is one ordered load per loop iteration. The scheduler.h and async_scheduler.h comments anddocs/STATE_EXEC_THREADED.md'sstop()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
state_exec_hazards_test12/12,state_exec_scheduler_test199/199 andstate_exec_async_test106/106 pass with--gtest_filter=-*Stress*(as the sanitizer legs run them).Hazards.ASchedulerStoppedFromAnotherThreadMayGoWhenItsRunReturnsalso passes 20 repeats of 2000 stop-then-destroy runs each. That run has no TSan, so it checks for regressions, not for the race.