Fix assertion race condition in #11800 #12707
Merged
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.
This PR fixes the race condition reported in #11800
We have also encountered it twice in
multicoretests
in ocaml-multicore/multicoretests#386 and ocaml-multicore/multicoretests#402.After a fair amount of head scratching, I realized that performing two consecutive atomic reads in the backup-thread state machine assertion
is a bad idea to execute in parallel with
backup_thread_func
that performs a state machine transition fromBT_TERMINATE
toBT_INIT
The reason is that there is a microscopic chance
di->backup_thread_msg
is firstBT_TERMINATE
causingdi->backup_thread_msg == BT_INIT
to be false,BT_INIT
in parallel bybackup_thread_func
, anddi->backup_thread_msg == BT_TERMINATE
is also false!This also explains why the bug has been close to impossible to reproduce.
Overall, it thus amounts to a buggy assertion and the fix is straightforward.
To confirm the above hypothesis, I first inserted a strategic
thrd_sleep
in the assertion:I then found that I could reliably trigger the bug on 5/5 runs on a torture test from
multicoretests
performing lots ofspawn
s andjoin
s when run with a reduced minor heap:I then inserted the patch with the
thrd_sleep
still in place, and reran the above test to confirm that 0 out of 10 consecutive runs would now trigger the assertion failure.