Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robust behavior when finalizers resurrect GC data (fixes #124) #184

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kvverti
Copy link

@kvverti kvverti commented Jan 30, 2025

  • Resurrected values take two garbage collection cycles to reclaim.
  • Finalizers are only run once even when the data is resurrected.

This is implemented through using a persistent bit that is set when a node's finalizer is run, and by running sweep() only on finalized nodes. During a GC cycle, the first mark() is used only to determine which nodes to finalize. The second mark() determines which nodes to reclaim.

Since the GC already did two mark phases, I thought it appropriate to keep a second mark phase for this implementation. It may be possible to defer reclamation until another GC cycle entirely, but that would require an equivalent to needs_drop for finalization to be efficient for empty finalizers, which appears to be non-trivial (or even impossible) without language support.

I also added a test that resurrects a GC in a finalizer and passes miri with these changes. I'm not aware of an easy way to produce a segfault in this scenario.

This reserves another bit in roots to store whether the node has been finalized. In a GC cycle, unmarked nodes are finalized at most once, and a second mark phase collects nodes that have not been ressurected.
@kvverti kvverti changed the title Robust behavior when finalizers ressurect GC data (fixes #124) Robust behavior when finalizers resurrect GC data (fixes #124) Jan 30, 2025
@kvverti kvverti marked this pull request as ready for review January 30, 2025 19:58
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.

1 participant