Skip to content

fix(android): stop fragment transition listeners from holding their transition - #11656

Merged
edusperoni merged 1 commit into
mainfrom
fix/android-transition-listener-cycle
Nov 22, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/android-transition-listener-cycle

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

Each fragment transition holds its TransitionListenerImpl from Java (Transition.mListeners), and the listener held that transition back as a JS property (public transition):

Transition --mListeners--> TransitionListenerImpl --(JS) transition--> Transition

#11136 made the listener's entry a WeakRef, which removed the path to the page, but this cycle remains. With the default wrapper lifetime, the runtime breaks it: once the listener's wrapper is unreachable from JS, it is resurrected and its Java object weakened, so the GC can collect both sides. A wrapper lifetime that keeps a JS-implemented object's state for as long as Java holds the object (the experimental canary mode in NativeScript/android#2127) cannot see that the cycle is garbage, so every navigation transition (the transition, its listener and their JS state) stays alive. On the automated suite this was the largest leak: at least 247 such cycles (the probe's search limit was hit), about 890 live CustomTransition wrappers at the end of the run.

What is the new behavior?

The listener no longer references its transition. addNativeTransitionListener returns { listener, transition }, and the entry keeps that pair in its *TransitionListener fields; _updateTransitions, _reverseTransitions, the transition-state clone and the clear functions read the transition from the pair and remove pair.listener. Everything the frame uses stays reachable from the backstack entry, as before, so nothing becomes weaker under resurrection.

New TRANSITIONS spec test_Transitions_SurviveGarbageCollectionBeforeGoingBack navigates forward with a transition, collects both heaps (gc() + java.lang.System.gc()), then goes back through the stored transitions.

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.
    Under canary, CustomTransition wrappers alive at the end of the run drop from about 890 to 100.

Related: #11136

…ransition

A fragment transition holds its TransitionListenerImpl in Java
(Transition.mListeners), and the listener held the transition back
through a JS property. With wrapper resurrection the cycle was broken
when the listener's wrapper was collected, but a runtime that keeps a
JS-implemented object's state for as long as Java holds the object can
never collect either side, so every navigation transition leaked.

The entry now keeps the listener and its transition side by side, and
the listener references neither the transition nor (since #11136) the
entry strongly.
@nx-cloud

nx-cloud Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit e7bc65a

Command Status Duration Result
nx run-many -t build -p core,webpack5,vite ✅ Succeeded 24s 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 19:58:26 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@11656
npm i https://pkg.pr.new/@nativescript/vite@11656
npm i https://pkg.pr.new/@nativescript/webpack@11656

commit: e7bc65a

@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.

Splitting { listener, transition } onto the backstack entry (AttachedTransitionListener) cleanly breaks the Transition <-> TransitionListenerImpl Java/JS cycle while keeping both strongly reachable for the lifetime of the entry across _getTransitionState / _restoreTransitionState and _clearFragment / _clearEntry.

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
@edusperoni
edusperoni merged commit 3174240 into main Nov 22, 2026
10 checks passed
@edusperoni
edusperoni deleted the fix/android-transition-listener-cycle branch November 22, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants