Repository navigation
Lock the GC root set while marking and visiting roots - #1438
Merged
Merged
Conversation
GCAddRoot, GCRemoveRoot and the offset root functions take sGCRootLock, but the collector iterated sgRootSet and sgOffsetRootSet without it. Stopping the world doesn't protect these sets, because threads in GC free zones and unregistered threads can still add and remove roots. 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.
GCAddRoot,GCRemoveRootand the offset root functions (GcAddOffsetRoot,GcSetOffsetRoot,GcRemoveOffsetRoot) takesGCRootLock. The collector iteratedsgRootSetandsgOffsetRootSetwithout it, both inMarkAlland when visiting roots.Stopping the world doesn't make that safe. Threads in a GC free zone, and threads that aren't registered at all, keep running during a collection and can still add and remove roots. If one does that while the collector is iterating, the iterator can be invalidated, or roots can be skipped and objects they reference collected. This came up in hxcpp_luv_io, where
uv_closecallbacks remove roots from a loop thread that is in a GC free zone (Aidan63/hxcpp_luv_io#1).This PR takes the lock around both iterations. Holding it can't deadlock. Threads that hold it only do
std::unordered_set/std::unordered_mapoperations, which never reach a GC safepoint, and the collector doesn't add or remove roots while it holds the lock.Tested with hxcoro's cpp test suite on Windows, with the default and threadpool dispatchers.
🤖 Generated with Claude Code