Repository navigation
Keep Ruby's GC disable state across a forced GC.start - #139
Merged
Merged
Conversation
eightbitraptor
force-pushed
the
mvh-fix-gc-start-disable-race
branch
2 times, most recently
from
November 5, 2026 11:35
a46aae8 to
5104295
Compare
eightbitraptor
force-pushed
the
mvh-fix-gc-start-disable-race
branch
from
November 5, 2026 13:36
5104295 to
a40f531
Compare
eightbitraptor
force-pushed
the
mvh-fix-gc-start-disable-race
branch
from
November 5, 2026 23:11
a40f531 to
8b1d37e
Compare
GC.start has to run even when GC is disabled, so we enable collection, run the GC and then disable it again. #134 fixed the crash we saw when another Ractor requested a GC before we could re-disable, but we still restore whatever state we saw before the GC. That's a problem when another Ractor changes the state in the meantime. If it enables GC while we're collecting, we disable it again on its behalf and GC stays off for good. Ractor.new disables and enables GC around thread allocation, so this isn't hard to hit. GC.disable and the internal gc_disable also share a single flag in mmtk-core, so one can undo the other. In this commit we track the internal flag, the user flag and any forced GC.start requests in the binding, and mirror the combined state into mmtk-core. A forced GC.start only enables collection until the next pause ends, and we reapply the state in resume_mutators before any mutator can request another GC. GC workers also toggle the internal flag during a pause, through DURING_GC_COULD_MALLOC_REGION. That reads the flag and then sets it, so two workers running in parallel can leave it set. mmtk-core used to ignore these because it can't disable collection mid-pause, so we do the same and drop internal changes between stop_all_mutators and resume_mutators. This replaces the retry loop from #134, if we fail to disable because a GC is pending, the end of that GC puts the state right. AtomicU64::update needs Rust 1.95, so we declare that in Cargo.toml.
eightbitraptor
force-pushed
the
mvh-fix-gc-start-disable-race
branch
from
November 6, 2026 18:26
8b1d37e to
567ed1e
Compare
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.
GC.start has to run even when GC is disabled, so we enable collection, run the GC and then disable it again. #134 fixed the crash we saw when another Ractor requested a GC before we could re-disable, but we still restore whatever state we saw before the GC.
That's a problem when another Ractor changes the state in the meantime. If it enables GC while we're collecting, we disable it again on its behalf and GC stays off for good. Ractor.new disables and enables GC around thread allocation, so this isn't hard to hit. GC.disable and the internal gc_disable also share a single flag in mmtk-core, so one can undo the other.
In this commit we track the internal flag, the user flag and any forced GC.start requests in the binding, and mirror the combined state into mmtk-core. A forced GC.start only enables collection until the next pause ends, and we reapply the state in resume_mutators before any mutator can request another GC.
This replaces the retry loop from #134, if we fail to disable because a GC is pending, the end of that GC puts the state right.