Skip to content

mark_sweep: run destructors for live values on collector drop#24

Open
Flamki wants to merge 5 commits intoboa-dev:mainfrom
Flamki:fix/collector-drop-destructors-20
Open

mark_sweep: run destructors for live values on collector drop#24
Flamki wants to merge 5 commits intoboa-dev:mainfrom
Flamki:fix/collector-drop-destructors-20

Conversation

@Flamki
Copy link
Contributor

@Flamki Flamki commented Mar 5, 2026

Closes #20

This patch fixes collector teardown behavior so tracked live allocations are properly finalized and dropped when MarkSweepGarbageCollector itself is dropped.

What changed

  • Added collector teardown cleanup to drain tracked queues and run finalization/drop hooks.
  • Updated GC and ephemeron drop hooks to drop inner values (not just mark them dropped).
  • Added regression test collector_drop_runs_destructors_for_live_gc_values.
  • Kept the change focused to mark-sweep internals and tests.

Why

Arena::drop deallocates backing memory directly. Without dropping tracked live values first, destructors for GC-managed types may never run at collector teardown.

Validation

  • cargo test -p oscars --lib -- --nocapture (33 passed)

@Flamki
Copy link
Contributor Author

Flamki commented Mar 9, 2026

Update from my side (no code review from maintainers yet, just technical status):

I rebased this branch onto latest main and resolved the mark-sweep merge conflicts.

I also kept the PR intentionally narrow. Relative to current main, this branch now changes only:

  • oscars/src/collectors/mark_sweep/mod.rs
  • oscars/src/collectors/mark_sweep/tests.rs

Technical behavior change:

  • In MarkSweepGarbageCollector::drop, I now check whether any queued GC nodes are still rooted (is_rooted() on root_queue and pending_root_queue).
  • If rooted values still exist, we keep the existing leak-protection path (avoid potential UAF).
  • If no rooted values remain, we run sweep_all_queues() so tracked GC/ephemeron allocations are dropped during collector teardown.

Regression coverage added:

  • collector_drop_runs_destructors_for_live_gc_values
  • collector_drop_runs_ephemeron_value_destructors_for_live_values

Validation run locally:

  • cargo fmt --all -- --check
  • cargo test --workspace (all passing: 48 tests in oscars + doc tests)

If you want, I can also split this into a follow-up cleanup PR, but functionally this branch is now focused on collector-drop teardown semantics.

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.

Arena::Drop leaks memory by failing to invoke destructors of GC objects

1 participant