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
multicoretestsin 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_functhat performs a state machine transition fromBT_TERMINATEtoBT_INITThe reason is that there is a microscopic chance
di->backup_thread_msgis firstBT_TERMINATEcausingdi->backup_thread_msg == BT_INITto be false,BT_INITin parallel bybackup_thread_func, anddi->backup_thread_msg == BT_TERMINATEis 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_sleepin the assertion:I then found that I could reliably trigger the bug on 5/5 runs on a torture test from
multicoretestsperforming lots ofspawns andjoins when run with a reduced minor heap:I then inserted the patch with the
thrd_sleepstill in place, and reran the above test to confirm that 0 out of 10 consecutive runs would now trigger the assertion failure.