Skip to content

fix(android): stop the setInterval runnable from capturing itself - #11659

Merged
edusperoni merged 1 commit into
mainfrom
fix/android-set-interval-self-reference
Nov 22, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/android-set-interval-self-reference

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

The Runnable that setInterval (@nativescript/core/timer) posts re-posted itself through a closure over its own runnable variable, so the runnable's implementation referenced the runnable:

Runnable --(JS) run--> closure --runnable--> Runnable

After clearInterval nothing else holds it. With the default wrapper lifetime, the runtime collects that cycle. A wrapper lifetime that keeps a JS-implemented object's state for as long as Java holds the object (the experimental canary mode, NativeScript/android#2127) keeps every cleared interval forever: 6 on the automated suite.

Reach is limited. Runtimes that provide their own timers skip core's polyfill, so this only affects code that imports @nativescript/core/timer or Utils.setInterval directly.

What is the new behavior?

The runnable re-posts the instance registered for its id (timeoutCallbacks[id]), which is present whenever it re-posts, since clearInterval removes it first. The runnable stays held by timeoutCallbacks while the interval is active, as before.

New TIMER spec test_setInterval_keepsFiringAcrossGarbageCollection collects both heaps from inside an interval callback and checks that the interval keeps firing.

Verified on an Android 15 (API 35) emulator against an Android runtime built from NativeScript/android#2130 (stacked on #2127), with this change and the other Android wrapper-lifetime fixes applied together:

  • "wrapperLifetime": { "mode": "resurrecting" } (today's default behavior, confirmed with getConfig("wrapperLifetimeMode")): full automated suite 1703 OK, 0 failed. The runtime counted 78,980 resurrections and 29,187 collected wrappers over the run, so the new specs ran while wrappers were being resurrected and their Java objects released.
  • "wrapperLifetime": { "mode": "canary", "scope": "all" }: full suite 1703 OK, 0 failed. After the run, findLeakCycles() reports 0 cycles, 0 leaked objects, compared with 1054 leaked objects on main.

The Runnable that setInterval posts re-posted itself through a closure
over its own variable, so the runnable's implementation referenced the
runnable. After clearInterval nothing else held it; wrapper resurrection
used to collect that cycle, but a runtime that keeps a JS-implemented
object's state for as long as Java holds the object keeps it forever.
The runnable now re-posts the instance registered for its id, which is
still present whenever it re-posts.
@nx-cloud

nx-cloud Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 8093c3b

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 29s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-20 22:57:41 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11659
npm i https://pkg.pr.new/@nativescript/vite@11659
npm i https://pkg.pr.new/@nativescript/webpack@11659

commit: 8093c3b

@edusperoni
edusperoni marked this pull request as ready for review November 20, 2026 21:45

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-posting timeoutCallbacks[id] instead of closing over runnable cleanly eliminates the self-referential Runnable -> closure -> runnable cycle after clearInterval, while if (timeoutCallbacks[id]) already guards active intervals and synchronous clearInterval calls inside the callback.

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
@edusperoni
edusperoni merged commit 1fd6bea into main Nov 22, 2026
10 of 12 checks passed
@edusperoni
edusperoni deleted the fix/android-set-interval-self-reference branch November 22, 2026 18:33
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.

3 participants