Skip to content

ui-react: pollers and elapsed tickers keep running while the app is hidden #2572

Open

Description

@schlessera

Context

Several timers in ui-react keep running while the installed app is hidden, or duplicate a shared ticker. Android resumes a backgrounded PWA rather than relaunching it, so the waste adds up over hours, and a return to the foreground waits for the next tick instead of refreshing at once.

  • Connectivity poller. useVpnStatus reschedules every 15 s, or every 3 s when the server is unreachable, whether the page is hidden or the device offline (packages/ui-react/src/hooks/use-vpn-status.ts:105-120, intervals at :9-12). It already re-checks on online and on becoming visible (:122-131), so hidden polling buys nothing. Each check builds an AbortController with a manual setTimeout/clearTimeout pair (fetchVpnStatus, use-vpn-status.ts:27-44) that AbortSignal.timeout() replaces.
  • Inbox badge poller. startInboxPolling loads the inbox every 60 s for the page's lifetime, hidden or not, and does not refresh when the page returns (packages/ui-react/src/stores/activity-state.ts:226-235, cleared at :315-316). The socket's liveness handling already shows the pattern to copy: visibilitychange, resume, freeze and pageshow (packages/ui-react/src/connection.ts:916-947).
  • Elapsed tickers. TurnStatus (packages/ui-react/src/components/chat/turn-status.tsx:26-32) and RunLanes (packages/ui-react/src/components/activity/run-lanes.tsx:54-61) each own a 1 s setInterval, although useNow (packages/ui-react/src/hooks/use-now.ts:13-32) exists to share one ticker between all such labels and is already used by four components. Its one shared interval takes the cadence of whichever subscriber mounted first (use-now.ts:17-21), and two existing consumers ask for 30-second updates (AGE_TICK_MS: packages/ui-react/src/components/chat/composer-row.tsx:68, packages/ui-react/src/components/chat/session-drawer.tsx:56). Moving the 1 s elapsed labels onto it unchanged would make them tick every 30 s whenever a 30 s consumer mounted first.
  • Transcription sync. Every RecordingItem in the transcribing state starts its own 2 s interval calling recordings.syncTranscriptions() (packages/ui-react/src/components/voice/recordings-tray.tsx:145-149), so three transcribing rows make three calls every 2 s for the same root-wide sync.
  • Storage guidance. createEvidence times its request out with a manual setTimeout(() => request.abort(), 10_000) (packages/ui-react/src/components/settings/storage/use-storage-guidance.ts:53) beside an abort controller that clear() also aborts.

Guide: modern-web-guidance performance: "DON'T block the thread with recurring timers: Avoid heavy polling with setInterval that starves the main thread."

Support (MDN BCD 8.1.6): AbortSignal.timeout is in Chrome for Android 124, Samsung Internet 27 and Firefox for Android 100; AbortSignal.any in 116, 24 and 124. freeze/resume are Chromium-only (Chrome for Android 68, Samsung Internet 10, not in Firefox), so visibilitychange stays the baseline and they are additive listeners only.

Binding: docs/decisions/session-principals.md:114 cites ui-react/src/hooks/use-vpn-status.ts:36 (res.status === 401). A PR that moves that line updates the citation (tests/decision-citations.test.ts).

Scope

  • useVpnStatus: do not schedule the next check while document.visibilityState === "hidden" or navigator.onLine === false. The existing visibilitychange and online handlers start it again. Replace the manual timeout in fetchVpnStatus with AbortSignal.timeout(TIMEOUT_MS).
  • activity-state.ts: stop the inbox interval on hidden and on freeze; on visible, resume or pageshow, load the inbox once and restart the interval. Remove the listeners when the store is disposed.
  • useNow: add an optional enabled argument (default true). Replace the single first-subscriber interval with one shared interval per distinct cadence: subscribers that ask for the same intervalMs share one timer, and each subscriber is driven only by its own cadence, so a 30 s consumer never re-renders every second and a 1 s consumer never waits 30 s, whatever the mount order. A cadence's interval exists only while it has at least one enabled subscriber and is cleared with its last one. When the page is hidden every cadence's interval stops; on becoming visible each active cadence ticks once immediately and resumes. TurnStatus and RunLanes use useNow(1000, measured) and useNow(1000, running) instead of their own intervals.
  • RecordingsTray: one interval for the tray, running while any listed row is transcribing and the existing per-row conditions (!busy && !offline && !localOnly) hold for at least one such row, paused while hidden. RecordingItem no longer starts one.
  • use-storage-guidance.ts: pass AbortSignal.any([request.signal, AbortSignal.timeout(10_000)]) to both calls and drop the manual timer.

Out of scope

  • Changing any interval length, the inbox API or the connectivity semantics (connected, unauthorized, unreachable).
  • connection.ts's socket liveness, which is already visibility-aware.
  • fetch priority hints (the modern-web-guidance deprioritize-background-fetches guide): api.activityInbox() takes no request options, and adding them is a separate change.
  • Background Sync or Periodic Background Sync.
  • Other setTimeout uses that are one-shot (focus moves, debounces).

Acceptance criteria

  • With the page hidden, no /api/vpn-check request is made after the in-flight one settles; becoming visible makes one immediately. Same for navigator.onLine === false and online. A fake-timer test fails before the change.
  • With the page hidden, the inbox poller makes no request; becoming visible makes one immediately and resumes the 60 s cadence. A freeze event stops it in the same way. Tested, failing before the change.
  • useNow mixed cadence, with fake timers, each failing on the current tree where it applies: a 30 s subscriber mounted before a 1 s subscriber leaves the 1 s subscriber updating every second and the 30 s one every 30 s; unmounting the 1 s subscriber leaves the 30 s cadence intact and clears the 1 s interval; unmounting the last subscriber leaves no timer; enabled: false subscribes nothing; hiding the page stops all cadences, and showing it ticks each active cadence once and resumes both at their own rates.
  • git grep -n "setInterval" packages/ui-react/src/components/chat/turn-status.tsx packages/ui-react/src/components/activity/run-lanes.tsx returns nothing, and their elapsed labels still advance each second while visible (existing tests or a new render test).
  • With three transcribing rows, syncTranscriptions is called once per 2 s, not three times. Tested.
  • A request that times out still reads as unreachable (VPN) or a failed guidance load, and clear() still aborts an in-flight guidance request.
  • Every listener added here is removed on unmount or dispose; a test asserts no timer remains after dispose.
  • Firefox for Android (no freeze/resume) pauses and resumes through visibilitychange alone.

Verification

bun run test packages/ui-react/tests
bun run typecheck
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

Not contract: no exported signature changes (useNow is internal, not exported from src/index.ts).

2026-10-24: specified useNow's cadence selection (one shared interval per distinct cadence) and its mixed-cadence tests, because the shared ticker took the first subscriber's interval and existing 30 s consumers would have slowed the 1 s elapsed labels.

Activity

  1. added
    type: fix
    area: ui-react
    priority: p3
    agent-ready
    on Oct 22, 2026
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

    agent-readyarea: ui-reactpriority: p3type: fix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions