Skip to content

End a backgrounded session when its timeout ran out, not when the device wakes #6585

Open

Description

@runningcode

When the app goes to the background, LifecycleWatcher schedules the session to end after sessionTrackingIntervalMillis (30s by default). The timer executor's delay doesn't count deep sleep. If the device sleeps during that window, the task only runs after the device wakes, and Session.end() stamps the wake time. The session's duration then includes the whole sleep, which can be hours.

Coming back to the app after the window has the same effect. startSession() ends the old session at the moment the app is foregrounded.

Fix

End the session at the moment its window ran out: the epoch time the app went to the background, plus the interval. This applies whether the scheduled task or the next foreground ends it.

Why v9

It shortens the reported duration of every session that slept through its background window. Session duration is a release-health metric.

Background

#6096 (#5576) is on 9.x.x. It decides session rotation on a monotonic Deadline, and it names this fix as its next step. No ticket tracked it until now.

Related, in the same class: isSessionOnScopeStale() still compares Session.getStarted() with the wall clock. A TODO [MAJOR] there asks for the session to remember the tick it started on, without serializing it.

Done when

  • A session that ends because its background window ran out is stamped at the end of the window.
  • Tests cover a deep-sleep delay: the ticker moves well past the window before the end task runs, and the session's end time doesn't move with it.
  • The v9 changelog lists it under Breaking Changes.
Agent notes

Verified on 9.x.x at 2033c63a5c (2026-11-12).

  • LifecycleWatcher.scheduleEndSession() creates sessionEnd = Deadline.after(ticker, sessionIntervalMillis) and schedules scopes.endSession() on getTimerExecutorService(). The comment there notes the executor's delay stops during suspend, while the deadline keeps counting.
  • startSession() takes the deadline. If it has passed, scopes.startSession() runs, and Scope.startSession() calls session.end() on the previous session (Scope.java:1076), which stamps DateUtils.getCurrentDateTime().
  • Session.end(Date) already takes a timestamp, but IScopes.endSession() and startSession() don't. Passing the due time through may need an internal overload.
  • LifecycleWatcher holds an EpochClock on 9.x.x, so the due instant can be epochClock.now() at onBackground() plus the interval.
  • Main still uses the wall clock for rotation. Fix this on 9.x.x only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions