Skip to content

Keep Ruby's GC disable state across a forced GC.start - #139

Merged
eightbitraptor merged 1 commit into
mainfrom
mvh-fix-gc-start-disable-race
Nov 6, 2026
Merged

eightbitraptor merged 1 commit into
mainfrom
mvh-fix-gc-start-disable-race

Conversation

@eightbitraptor

@eightbitraptor eightbitraptor commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

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.

@eightbitraptor
eightbitraptor force-pushed the mvh-fix-gc-start-disable-race branch 2 times, most recently from a46aae8 to 5104295 Compare November 5, 2026 11:35
@eightbitraptor
eightbitraptor force-pushed the mvh-fix-gc-start-disable-race branch from 5104295 to a40f531 Compare November 5, 2026 13:36

@k0kubun k0kubun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks 🙏

@eightbitraptor
eightbitraptor force-pushed the mvh-fix-gc-start-disable-race branch from a40f531 to 8b1d37e Compare November 5, 2026 23:11
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
eightbitraptor force-pushed the mvh-fix-gc-start-disable-race branch from 8b1d37e to 567ed1e Compare November 6, 2026 18:26
@eightbitraptor
eightbitraptor merged commit 7430ebe into main Nov 6, 2026
30 checks passed
@eightbitraptor
eightbitraptor deleted the mvh-fix-gc-start-disable-race branch November 6, 2026 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants