Skip to content

Pushed pages never run OnDisappearing on a tab switch, so in-flight loads are not cancelled #132

Closed

Description

@zhollis21

Problem

OnDisappearing never fires on the five pushed pages when you switch tabs away from them, so the
cleanup it performs simply does not happen. Found while fixing #127, which was the other half of
the same root cause.

Detail pages and View All are pushed onto a tab's navigation stack (AppShell.xaml.cs:23-27), and
MAUI Shell gives a page in a backgrounded stack no appearing/disappearing when the tab changes.
Measured on device, two tab round-trips away from a media details page:

Hook Fired
OnAppearing 1 (initial navigation only)
OnDisappearing 0
Loaded / Unloaded 1 / 0
OnNavigatedTo / OnNavigatedFrom 3 / 2

#127 fixed the appearing half by moving the display-settings re-projection to OnNavigatedTo. The
disappearing half is untouched.

What does not run

MediaDetailsPage.OnDisappearing (and the equivalent on StaffDetailsPage,
CharacterDetailsPage, StudioDetailsPage, MediaBrowsePage):

protected override void OnDisappearing()
{
    base.OnDisappearing();
    _loader.OnDisappearing();
    // Abandon any in-flight load / section fetch so we don't keep hitting the API after navigating
    // away. ...
    ViewModel.CancelInFlight();
}

ViewModel.CancelInFlight() — the comment states the intent plainly, and on a tab switch it is
not honoured. Tab away from a details page mid-load and the media fetch, and any Load More on the
Characters / Staff / Recommendations sections, run to completion against AniList. That spends
rate-limit budget on a page the user has left. It also means PageLoadScope is never cancelled, so
the guards written against it are inert on this path.

_loader.OnDisappearing() — leaves DeferredContentLoader._hasAppeared true and
_pendingVersion un-bumped. The loader believes the page is still on screen after the user has left,
so IsCurrent(version) keeps returning true and a scheduled load that yields will proceed rather
than no-op. The "supersede any scheduled/in-flight load so it no-ops after the yield" comment on that
line describes behaviour that does not occur for tab switches.

Fix shape

OnNavigatedFrom is the counterpart to the OnNavigatedTo that #127 now uses, and it fired reliably
in the same measurement (2 for 2). Moving the two cleanup calls there is the obvious change. It wants
care rather than a blind move:

  • OnNavigatedFrom also fires on a forward navigation (details → staff details), where cancelling
    in-flight work is correct and already happens today via OnDisappearing. Confirm the two do not
    double-cancel or cancel something the destination page needs.
  • The sort popup currently relies on OnAppearing firing when it closes (see the reuse-guard comment
    in DetailsPageModelBase.LoadCoreAsync). Check whether a popup open/close also fires
    OnNavigatedFrom/OnNavigatedTo; if it does, the popup path may start cancelling loads it did not
    before.
  • Decide whether OnDisappearing should keep its calls as well. It still fires on back-navigation and
    popup dismissal, so removing them outright would change those paths too.

Verify on device, not in tests

No unit test can catch this, for the same reason it could not catch #127: the page model cancels
correctly when told to, and the defect is that nothing tells it. See #131.

A repro worth capturing: open a details page on a slow connection, tab away while the spinner is up,
and watch LoggingHandler's HTTP POST/HTTP 200 lines continue after the switch.

Open question — do tab-root pages get OnDisappearing?

Not measured. It matters beyond the pushed pages: SettingsPage.OnDisappearing is what calls
FlushPendingSaveAsync, the mechanism that sends a settings change still sitting in its 1500 ms
debounce when the user tabs away (#118, #128). If tab-root pages have the same gap, that flush never
runs and a change made and tabbed away from within the debounce window relies solely on the timer.
Worth measuring before assuming the tab-root half is sound.

Related

Activity

  1. added
    bug
    on Aug 24, 2026
  2. zhollis21 commented on Aug 25, 2026

    @zhollis21
    OwnerAuthor

    Open question answered: tab-root pages do get OnDisappearing. The gap is specific to pushed pages.

    Measured on device — changed a setting on Settings, tabbed away 438 ms later (well inside the 1500 ms debounce):

    20:00:45.151  tap Romaji
    20:00:45.589  tap Library tab
    20:00:45.622  GraphQL UpdateUser request     <- 33 ms after the tab switch
    20:00:45.982  HTTP 200 in 357ms
    

    The save fired on the tab switch, not on the debounce timer, which would have landed around 20:00:46.65. So SettingsPage.OnDisappearing runs and FlushPendingSaveAsync works as designed. #118 and #128 are unaffected by this issue.

    That narrows the scope here to the five pushed pages only, and leaves the ViewModel.CancelInFlight() and _loader.OnDisappearing() calls as the live problem.

  3. zhollis21 commented on Aug 25, 2026

    @zhollis21
    OwnerAuthor

    Verified against fe4dbad (main, after #133 merged). Updated after measuring on device — the open
    questions below are now answered.

    Confirmed as filed. All five pushed pages call _loader.OnDisappearing() + ViewModel.CancelInFlight()
    from OnDisappearing and nothing else: MediaDetailsPage.xaml.cs:117-124, StaffDetailsPage.xaml.cs:81-88,
    CharacterDetailsPage.xaml.cs:81, StudioDetailsPage.xaml.cs:81, MediaBrowsePage.xaml.cs:105-109.
    All five already override OnNavigatedTo, and it does one thing — ViewModel.RefreshDisplaySettings()
    (#127). None overrides OnNavigatedFrom.

    Full hook table, measured on device (Debug + -p:CiBuild=true, temporary logging in all four
    hooks on all five pushed pages plus SettingsPage as a control):

    Path OnAppearing OnDisappearing OnNavigatedTo OnNavigatedFrom
    Initial navigation yes — yes —
    Tab away (pushed page) — NO — yes
    Tab back (pushed page) NO — yes —
    Forward nav to sub-page — yes — yes
    Back nav from sub-page yes — yes —
    Sort popup open — yes — yes
    Sort popup close yes — yes —
    Tab-root page (Settings) away — yes — yes
    Tab-root page (Settings) to yes — yes —

    The navigation hooks are a strict superset: they fire everywhere the appearing hooks do, plus the
    two tab-switch rows where those do not. So moving the cleanup changes no other path.

    The two open questions in the body are answered. The sort popup fires both pairs — open fires
    OnDisappearing and OnNavigatedFrom, close fires OnAppearing and OnNavigatedTo — so
    moving to the navigation hooks leaves the popup path behaving exactly as it does today. Forward
    navigation likewise fires both, and the double-cancel it raises is already absorbed by
    PageLoadScope.EnsureActive() (PageLoadScope.cs) plus the fresh token from Scope.Begin()
    (DetailsPageModelBase.cs:277).

    DeferredContentLoader is not symmetric across the two pairs, which constrains the fix.
    OnAppearing() sets _hasAppeared = true; OnDisappearing() clears it. Both IsCurrent(version)
    and TrySchedule gate on it. Since OnAppearing does not fire on a tab return, moving
    _loader.OnDisappearing() to OnNavigatedFrom alone would leave _hasAppeared false with no
    hook to set it back — after one tab round-trip TrySchedule would return early every time and the
    page would sit on its spinner permanently. The cleanup has to be paired with a re-arm from
    OnNavigatedTo.

    Repro tooling. #125 landed runtime fault injection; driver.ps1 fault GetMedia delay next -delay 8000
    holds a load open long enough to tab away mid-flight, which is what makes this reproducible on demand.

  4. added
    p3-medium
    on Aug 26, 2026
  5. zhollis21 commented on Aug 27, 2026

    @zhollis21
    OwnerAuthor

    Shipped in #135 (925d79f). Verified on main: a pushed page's tab switch now routes through _loader.OnDisappearing() (src/AniSprinkles/Pages/MediaDetailsPage.xaml.cs:117, with the lifecycle asymmetry documented at :98), and DeferredContentLoader.OnDisappearing clears _hasAppeared and bumps _pendingVersion so any scheduled or in-flight load no-ops after its yield (src/AniSprinkles/Utilities/DeferredContentLoader.cs:61-65).

    Note #131 remains open and is the reason this could not be caught automatically — nothing drives a real tab switch against a pushed detail page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugp3-medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions