Skip to content

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

Merged
edusperoni merged 1 commit into
mainfrom
fix/leak-probe-review
Nov 21, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/leak-probe-review

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

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.

Summary by CodeRabbit

  • Bug Fixes

    • Leak detection now accounts for queued wrapper releases and objects retained by native code, reducing false leak reports.
    • Canary-state cleanup is reflected in leak snapshots, improving cycle and object-lifetime reporting.
  • Documentation

    • Clarified how leak-cycle snapshots handle collections, queued releases, retained wrappers, and inspection limits.

- 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

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The leak probe now accounts for queued wrapper releases, resurrected wrappers, and canary-state handles when building graphs and reporting cycles. It settles queued releases and dead canary states before taking leak-cycle snapshots.

Changes: Leak probe ownership and canary-state accounting

Layer / File(s) Summary
Expose and drain queued releases and canary states
NativeScript/runtime/LeakProbe.mm, NativeScript/runtime/LeakProbeSPI.h, NativeScript/runtime/WrapperLifetime.h, NativeScript/runtime/WrapperLifetime.mm, NativeScript/runtime/WrapperLifetimeCanary.h, NativeScript/runtime/WrapperLifetimeCanary.mm
The SPI advances to version 3 and exposes queued-release counts, resurrection state, and canary-state handles. Runtime iteration and host draining include queued wrapper releases and dead canary states.
Model retained wrappers and canary handles
NativeScriptLeakProbe/NativeScriptLeakProbe.mm
The probe models queued retains and canary-state handles, adds ns:kept edges for resurrecting wrappers, and treats ns:owned and ns:kept edges consistently in cycle detection.
Settle snapshots and verify leak reports
NativeScriptLeakProbe/NativeScriptLeakProbe.mm, TestRunner/app/tests/WrapperLifetimeLeaksTests.js, docs/ns-builtin-modules.md
Leak-cycle snapshots and count reports use a shared settling routine. Tests and documentation cover retained objects, canary behavior, and snapshot processing.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related PRs

  • NativeScript/ios#576: Introduced the leak probe and graph used by this PR’s SPI and graph changes.
  • NativeScript/ios#561: Introduced canary wrapper-lifetime mode and state objects, which this PR handles during settling and graph construction.
  • NativeScript/ios#560: Introduced deferred native releases that this PR counts and drains.

Suggested reviewers: nathanwalker

Merge Risk: Minimal · up to 563b0

This updates the leak probe's accounting for queued wrapper releases, resurrected wrappers and canary state. It affects diagnostic tooling only, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(runtime): leak probe review fixes" is related to the changeset. The PR objectives describe a set of fixes found during review of leak-probe behavior: settling before snapshots, queued-r…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(runtime): leak probe review fixes" is related to the changeset. The PR objectives describe a set of fixes found during review of leak-probe behavior: settling before snapshots, queued-release accounting, the ns:kept edge, canary state handles, and the SPI version bump to 3. The title is broad and uses the generic phrase "review fixes", so it names the area but not the primary change. It does not describe something absent from the changeset, so it does not fail. It is not so vague that it conveys no information, so it is not inconclusive.

  • 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

A rabbit checks the wrapper trail,
Queued releases leave the tail.
Canary states are counted too,
The graph records each edge in view.
Three collections, then a scan,
The rabbit hops: “That’s the plan!”

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6cb8916b-2d53-4870-873f-a297161a1ee5
Commits

Reviewing files that changed from the base of the PR and between cb0e0f0 and 563b0d3.

Files selected for processing: 9

  • NativeScript/runtime/LeakProbe.mm
  • NativeScript/runtime/LeakProbeSPI.h
  • NativeScript/runtime/WrapperLifetime.h
  • NativeScript/runtime/WrapperLifetime.mm
  • NativeScript/runtime/WrapperLifetimeCanary.h
  • NativeScript/runtime/WrapperLifetimeCanary.mm
  • NativeScriptLeakProbe/NativeScriptLeakProbe.mm
  • TestRunner/app/tests/WrapperLifetimeLeaksTests.js
  • docs/ns-builtin-modules.md

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Finishing Touches
Fix failing CI checks

  • Commit to this branch
  • Create a new PR

@edusperoni
edusperoni marked this pull request as ready for review November 21, 2026 19:48
@edusperoni
edusperoni merged commit ba67586 into main Nov 21, 2026
10 of 11 checks passed
@edusperoni
edusperoni deleted the fix/leak-probe-review branch November 21, 2026 20:42
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