Repository navigation
fix(android): stop the setInterval runnable from capturing itself - #11659
Merged
Merged
Conversation
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.
|
View your CI Pipeline Execution ↗ for commit 8093c3b
💡 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 |
commit: |
edusperoni
marked this pull request as ready for review
November 20, 2026 21:45
NathanWalker
approved these changes
Nov 21, 2026
NathanWalker
left a comment
Contributor
There was a problem hiding this comment.
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.
farfromrefug
approved these changes
Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
What is the current behavior?
The
RunnablethatsetInterval(@nativescript/core/timer) posts re-posted itself through a closure over its ownrunnablevariable, so the runnable's implementation referenced the runnable:After
clearIntervalnothing 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 experimentalcanarymode, 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/timerorUtils.setIntervaldirectly.What is the new behavior?
The runnable re-posts the instance registered for its id (
timeoutCallbacks[id]), which is present whenever it re-posts, sinceclearIntervalremoves it first. The runnable stays held bytimeoutCallbackswhile the interval is active, as before.New TIMER spec
test_setInterval_keepsFiringAcrossGarbageCollectioncollects 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 withgetConfig("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 onmain.