Skip to content

Feature/jrc 8103.fix.api.34.heuristic#8317

Open
jrodiz wants to merge 4 commits into
mainfrom
feature/jrc--8103.Fix.API.34.heuristic
Open

Feature/jrc 8103.fix.api.34.heuristic#8317
jrodiz wants to merge 4 commits into
mainfrom
feature/jrc--8103.Fix.API.34.heuristic

Conversation

@jrodiz

@jrodiz jrodiz commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

firebase-perf: fix API 34+ background-start heuristic in AppStartTrace

Fixes #8103 (follow-up to b/339891952).

AppStartTrace's background-start detection relied on the ordering of a posted main-thread runnable vs the first onActivityCreated. On API 34+ the OS can drain the runnable before delivering the activity-launch Binder transaction even on a genuine cold foreground launch — the gap on real Pixel devices is ~200–300 ms, well above the previous 50 ms tolerance. Every affected user lost their _app_start traces.

Fix

On API 34+, resolveIsStartedFromBackground switches from the timing-window heuristic to an OS-reported process start cause. Pre-API-34 is unchanged.

API <34: legacy mainThreadRunnableTime check (unchanged).
API 34+: AppStartCause.cause
         FOREGROUND ⇒ log.
         UNKNOWN / null ⇒ suppress.

The new AppStartCause helper reads RunningAppProcessInfo.importance via ActivityManager.getMyMemoryState once, at first capture inside AppStartTrace.registerActivityLifecycleCallbacks. IMPORTANCE_FOREGROUNDFOREGROUND (activity-driven start); anything else ⇒ UNKNOWN. Pre-API-34 returns UNKNOWN so the legacy decision stays in charge. FirebasePerfEarly stops scheduling StartFromBackgroundRunnable on API 34+ since its output is no longer consumed.

Behavior matrix

Scenario API <34 API 34+
Foreground launcher tap log (unchanged) log via FOREGROUND cause
Background broadcast (no activity) n/a n/a
Warm start (broadcast → activity) suppress (unchanged) suppress via UNKNOWN cause

Tests

./gradlew :firebase-perf:spotlessApply :firebase-perf:testReleaseUnitTest — 0 failures. AppStartTraceTest 9/9 (@Config(sdk = 33) for the pre-API-34 path; FOREGROUND / UNKNOWN / null for API 34+). AppStartCauseTest 6/6 (null-context, pre-API-34, API 34 FOREGROUND / SERVICE / CACHED importance branches, constructor).
Test project app: https://github.com/jrodiz/firebase-perf-e2e-for-issue8103

Risks

  • API 34+ behavior changes for every app: timing-window heuristic gone, importance-based signal wired in. _app_start emission rate rises on the previously-broken population — the intent.
  • RunningAppProcessInfo.importance is stable public SDK and a single Binder IPC; no reflection.
  • Pre-API-34 path untouched.

jrodiz and others added 4 commits June 5, 2026 10:05
#8103)

Replace the timing-window heuristic in resolveIsStartedFromBackground on
API 34+ with an OS-reported process start cause. Pre-API-34 keeps the
legacy runnable-before-onActivityCreated check.

  API < 34: mainThreadRunnableTime set first ⇒ suppress (legacy, unchanged).
  API 34+:  ProcessStartCause.cause
              FOREGROUND ⇒ log.
              UNKNOWN / null ⇒ suppress.

New ProcessStartCause helper reads RunningAppProcessInfo.importance at
first capture via ActivityManager.getMyMemoryState. IMPORTANCE_FOREGROUND
indicates an activity-driven start; anything else maps to UNKNOWN.
Pre-API-34 returns UNKNOWN and the legacy AppStartTrace logic owns the
decision.

FirebasePerfEarly stops scheduling StartFromBackgroundRunnable on API 34+
since its output is no longer consumed there.

:firebase-perf:testReleaseUnitTest — 0 failures / 0 errors.
AppStartTraceTest 9/9, ProcessStartCauseTest 6/6.
Rename ProcessStartCause → AppStartCause (class, field, test helpers/class) per review, and document resolveIsStartedFromBackground: the pre-API-34 branch is the only one that checks/clears mainThreadRunnable; the API 34+ branch is driven by the OS-reported cause.
@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@github-actions

Copy link
Copy Markdown
Contributor

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

@jrodiz jrodiz requested a review from tejasd June 17, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[firebase-perf] _app_start suppressed on API >=34 for large apps — 50ms MAX_BACKGROUND_RUNNABLE_DELAY threshold insufficient (b/339891952 follow-up)

2 participants