Conversation
compare_exchange_strong writes the current value into expected when it fails. The spin loops kept that value, so the next attempt compared against 1 and succeeded while another thread still held the lock. Two marking threads could then pop the same chunk list at once, and a chunk could be lost: its objects were marked but never traversed, and what only they referenced was freed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
#1389 turned the two pop spin locks in
GlobalChunksinto:compare_exchange_strongstores the current value inexpectedwhen it fails. After the first failed attemptexpectedis 1, so the next attempt compares the lock against 1 and succeeds while another thread still holds it. The old_hx_atomic_compare_exchange(&lock, 0, 1)compared against 0 every time.With the lock no longer exclusive, two marking threads can pop
processList(orfreeList) at once, which is the ABA case the lock exists to prevent: a thread'scompare_exchangecan move the head onto a chunk another thread has just taken, so a chunk is processed twice or lost. A lost chunk leaves objects marked but never traversed, and whatever only they reference is freed.We hit this in a game built with
HXCPP_GC_GENERATIONALafter updating hxcpp past #1389: intermittent crashes on native access to freed memory (aMovieClip's__childrenbuffer). WithHXCPP_GC_CHECK_POINTERthe first bad access wasOld object accesson a field of an object whose own access had just passed the check, i.e. a marked object whose child had not been marked. The crash does not occur with marking on a single thread, nor with hxcpp from before #1389, nor with this change; I could not reproduce it in a standalone stress test, the interleaving it needs being rare outside the game.The fix resets
expectedto 0 on every spin, in both loops.mZeroLockandgPauseForCollectuse a single attempt and are unaffected.