Skip to content

fix(runtime): leak probe review fixes - #591

Draft
edusperoni wants to merge 1 commit into
mainfrom
fix/leak-probe-review
Draft

edusperoni wants to merge 1 commit into
mainfrom
fix/leak-probe-review

Conversation

@edusperoni

Copy link
Copy Markdown
Collaborator

Fixes in the leak probe (#576). These are issues the Android port's review found (NativeScript/android#2130), checked against the iOS code. Each new spec fails without its fix.

Fixes

  • Settle before every snapshot.
    • Before: findLeakCycles() and the cycles and strict Darwin triggers took the snapshot right away; only the counts and inspect triggers settled.
    • The problem in canary mode: a canary that died in the snapshot's own GC has its handle reset while its Instances entry and the runtime's queued +1 remain. Its object was listed as changed and lost its root status, so cycles through an outside-owned object came back uncertain with the wrong reason. In rooted mode the queued +1 read as an outside owner and hid cycles.
    • The fix: every entry point now runs three full collections, each followed by the queued releases and a synchronous drain of deallocated canary records. Settling can run JS -deallocs; the docs say so.
    • On top of that, a new Instance.queued field gives the count of retains still queued for dead wrappers, per object. A queued seed is not marked changed, and its pending retains count as the runtime's.
  • ns:kept edge. In resurrecting mode, a TS/@NativeClass-extended wrapper that native code retains is GC-protected but sits behind a weak handle. The graph had no edge from its object to it, so cycles through the wrapper's JS were reported while the object was owned from outside. The object now has an ns:kept edge to the wrapper, excluded from the two-node loop check like ns:owned.
  • Dead canary states are not roots. The state handles of canary records whose object is being or was deallocated are still strong until the posted drain. They were never modeled, so they counted as JS roots and hid cycles through shared owners. A host visitor now hands live, dying and dead records' handles to the probe.
  • Handle scopes. EdgeName and NodeName open their own HandleScope, so the walk no longer accumulates a handle per edge.
  • LeakProbeSPI.h goes to version 3, for the new Instance.queued field and host visitor.

Not affected on iOS, unlike Android: the "(Global handles)" edge filter, the ClearObjectIds guard (iOS checks whether the context already had an id), the ephemeron edge prefix, and truncation.

A JS block whose kBlockDeallocating bit is set while it waits for the runtime's lock still reads as a root. That is documented as a limitation.

Specs (WrapperLifetimeLeaksTests.js)

  • canary only: an object whose canary just died, probed right after __collect() with no settle, is not changed and has no cycle reported.
  • canary only: a cycle shared with an owner released in the same turn is still found.
  • Every mode: a TS-extended object (__extends) held by an owner the probe can't see, with a JS ↔ block cycle through its wrapper, is not reported.

Testing

2161 specs, 0 failures in resurrecting/extended, rooted/all, canary/all, canary/extended, and canary/all under ASan. Specs (1) and (3) failed on main under canary; spec (2) failed on main in default mode.

- Settle before every snapshot: findLeakCycles and the cycles, strict and
  inspect triggers run three full collections, each followed by the queued
  releases and a synchronous drain of deallocated canary records. A wrapper
  that dies in the snapshot's own collection reports its queued retains
  (Instance.queued), so its object is neither listed as changed nor stripped
  of its root status, and the pending retain counts as the runtime's.
- Model the object a resurrecting wrapper lives for: an ns:kept edge from the
  object to a GC-protected wrapper behind a weak handle, excluded from the
  two-node loop check like ns:owned.
- The state handles of canary records whose object is being or was
  deallocated are the runtime's handles, not roots.
- Scope the handles EdgeName and NodeName make.
- LeakProbe SPI version 3.
- Docs: settling, ns:kept, and the JS block disposal race as a limitation.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch has not been deployed

No deployments
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