AX Notes · Incident

GMA Next-Gen called our full-screen ad callback on a background thread: how a photo app crashed after 243 passing unit tests

Navigating inside an ad SDK callback can crash your app. GMA Next-Gen delivered our interstitial callback on a background thread, and 243 unit tests missed it.

By Stephen Play Store and monetization 10 min read Last updated

Three photo apps, the first 20 days 2/4

Four minutes after 243 unit tests finished with zero failures, Compress Image dropped to the launcher on the emulator when we tapped Start over after a second job. That was 16:10 on September 23, 2026, and the crash log named the thread GMA(BG) 4. We hit two traps that afternoon while adding three ad formats. One is this crash. The other is that UMP, Google's ad consent SDK, returned false from canRequestAds() at every launch, so the splash screen never waited for an app open ad. Both were fixed that afternoon.

The same app also crashed that night because R8 stripped a constructor; that story is in What to check before shipping an AI-built Android app. How often our block puzzle game shows interstitials is a separate note: We set interstitial frequency from device timelines. Both defects here appeared in an unminified debug build and have nothing to do with frequency rules.

Sources: the app repository's git history and changelog, the ad design document, code and tests, that day's work log and three review reports (Korea time, checked November 8, 2026).

Contents (9)
  1. popBackStack threw on GMA(BG) 4
  2. A review fix moved navigation into the callback
  3. Why 243 tests and the third review missed it
  4. All four callbacks now go through one main-looper Handler
  5. UMP starts every cold launch with canRequestAds() false
  6. Measured only on an API 35 emulator with no consent required
  7. Five steps before hooking an SDK callback
  8. No test guards the main-looper post yet
  9. FAQ

popBackStack threw on GMA(BG) 4

The crash was caught by a journey script the agent wrote that afternoon. It launches the debug build against AdMob test ad units and walks six steps (listed in step 4 of the procedure below). Step 6 finishes the second job and taps Start over, the first place an interstitial can appear. In the first journey, finished at 15:31, the interstitial showed there without trouble. In the 16:10 run the app vanished and left the launcher on screen, so the agent read the crash buffer.

FATAL EXCEPTION: GMA(BG) 4
java.lang.IllegalStateException: Method setCurrentState must be called on the main thread
  at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.kt:382)
  at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.kt:139)
  at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.kt:161)
  ...
  at androidx.navigation.NavController.popBackStack(NavController.android.kt:244)
  at ...SugarCompressNavHost...(SugarCompressNavHost.kt:135)

The result screen's Start over handler called popBackStack() to return HOME. Navigation then changed the lifecycle of the screen being popped, and LifecycleRegistry refused because it was not on the main thread. The caller was the SDK's onAdShowedFullScreenContent callback, which, in our log, GMA Next-Gen invoked on its own background thread. Nothing caught the exception, so the whole process died.

Google Play screenshot of the Compress Image result screen: 8.58 MB down to 1.44 MB, with before and after sizes for three photos.
The Compress Image result screen (Google Play screenshot). The crash came when leaving this screen with Start over, the spot where an interstitial can appear (the button is cropped out of the screenshot).

A review fix moved navigation into the callback

The first interstitial code, written the night before, received the same callback and never crashed. All the callback did was complete a CompletableDeferred, which is safe from any thread. A commit at 00:26 on September 23 confirmed on the emulator that an interstitial showed after the third job and that closing it returned to HOME. Back then the app navigated after the ad closed.

On the afternoon of September 23 the operator proposed shipping all three ad formats at once, and asked for a version that had been analyzed in depth and cross-checked. That proposal and request are the operator's whole part on record; no person edited the code afterward. The agent brought in a separate reviewer agent told to report, not edit (it only ever read code). By then the working tree navigated to HOME first and showed the ad second. The second review, at 16:02, said HOME was drawn and tappable before the interstitial appeared. That is the "ad appears unexpectedly while the user is interacting" pattern AdMob warns about, and a quick tap could open the photo picker together with the ad. The reviewer's fix: stay on the result screen and navigate to HOME the moment the ad covers it.

The agent took that moment from the SDK's onAdShowedFullScreenContent and passed the navigation function into it. Both installing the callback and calling show() happen inside withContext(Dispatchers.Main.immediate), which fixes the thread of the code doing the installing, not the thread the SDK later calls back on.

The banner loader written on September 20 had posted adView.destroy() to a main-looper Handler from day one. That handling never made it to the full-screen code.

Why 243 tests and the third review missed it

A unit test covered exactly this behavior. FullScreenControllersTest has a test that checks navigation happens only once the ad covers the screen ("Start over navigates only once the ad covers the screen, or right away without one"). Its fake ad, though, calls onAppeared() directly on the test thread. A single-threaded fake cannot reproduce a threading bug, so the test passed.

The first and second reviews came before the callback navigated at all; only the third saw that code. At 16:07 it marked "Navigate under the ad: resolved", and opened its report by saying it had only read code and had built or run nothing. The crash three minutes later is the log above.

The cross-check found the eight pacing defects the CHANGELOG lists, including an interstitial after a failed job, a retried job counted twice, ad requests for a Pro user with a stale cache, and an ad shown on a stopped Activity.

We don't call an ad build done until it has run on an emulator or device up to the point where a real ad appears.

All four callbacks now go through one main-looper Handler

The 16:12 threading fix lives in one file, GmaFullScreen.kt, which interstitials and app open ads share (other third-review findings were fixed across several files at the same time). The shown, failed-to-show, dismissed and paid callbacks all hand their work to the same main-looper Handler.

private val mainHandler by lazy { Handler(Looper.getMainLooper()) }

onShowed = {
    mainHandler.post {
        onAppeared()          // navigates to HOME here
        appeared.complete(true)
    }
},
onDismissed = {
    mainHandler.post {
        onDismissed()
        dismissed.complete(true)
    }
},

Posting only the shown callback would have stopped the crash. The code comment gives the reason for moving all four: queued on one looper in arrival order, "shown" is always handled before "dismissed". Going back to the pre-review order, navigating first and showing the ad second, was off the table, since the second review had flagged it as a policy risk.

The 16:17 rerun reached step 6, showed the ad screen (AdActivity), and returned to the app after it closed. The crash buffer had no FATAL entries. The ad design document gained a line saying anything new hooked to an SDK callback must assume a background thread.

UMP starts every cold launch with canRequestAds() false

The UMP problem showed up about 40 minutes before the crash. In the first journey, finished at 15:31, the cold-start app open ad never held the splash. A 15:33 cold start with added logging printed splash not held: BlockedNoConsent. Holding the splash depended on consent state, and a code comment assumed that in Korea, where no consent form is required, consent from the previous session would already allow ads.

The agent's next suspect was UMP's debug settings. At 15:45 it cold-started a build with them and a build without them, twice each. All four runs began with canRequestAds=false and status 0 (UNKNOWN). The consent update took 2,246 to 2,413 ms, SDK initialization another 1,266 to 1,412 ms, finishing 4,767 to 5,116 ms after process start. All four timed out the 2 s wait with "consent/SDK not ready in 2000 ms". UMP did not carry "consent not required" into the next launch, so every cold start paid the round trip again.

Bar chart: the consent round trip took 2,246 to 2,413 ms in all four runs, over the old 2,000 ms budget; the budget became 3,000 ms.
Every cold start pays the consent round trip again, so the SDK could not even start initializing within 2 s.

A new AdGate.wants() decides whether to hold the splash from the Pro state and the feature flag alone. Actual ad requests still go through decide(), which checks consent and initialization first. The wait went from 2 s to 3 s. The 2 s came from our Korean spell-check app, which initializes the SDK before consent, an order this app's ad rules forbid. Google's app open sample waits 5 s. A fill that arrives after the budget is kept for the next return.

Even 3 s was not enough on the emulator. The final 16:25 journey logged "consent/SDK not ready in 3000 ms" at step 2, with the Play purchase check taking 4,139 ms. The ad design document records this limit with the emulator's 3.4 to 4.1 s for the purchase check.

Measured only on an API 35 emulator with no consent required

We have not measured the startup state or round trip in the EEA or other regions where the consent form appears, and we have not checked callback threads in the older, pre-Next-Gen GMA SDK. Google's Next-Gen interstitial page and migration guide don't say which thread callbacks arrive on (checked November 8, 2026). The only threading advice in the migration guide is to initialize the SDK on a background thread.

Code whose callbacks only do thread-safe work, like the first interstitial code above, doesn't need this fix. The ad design document's October 4 notes say a sideloaded Galaxy S20 got its Play answer in 3.34 s, so it showed no cold-start ad either way, while a Galaxy S21 showed cold-start ads from the second launch. The S21's Play latency wasn't measured.

Five steps before hooking an SDK callback

  1. Find every ad SDK callback body and mark calls that touch navigation, lifecycle or views. rg -n "onAd(Showed|Dismissed|FailedToShow)FullScreenContent" is a start.
  2. Post the marked calls to a main-looper Handler, and send all callbacks of one ad through the same Handler so their order holds.
  3. Add a test that fires callbacks from another thread. A controller-level fake skips everything below the Handler, so this has to be a Robolectric test that calls the SDK-side function, GmaFullScreen here, directly. The module has no Robolectric dependency yet, and the function waits for dismissal, so the thread should call onDismissed() too. An untested sketch:
    var onMain: Boolean? = null
    // fire shown and dismissed from another thread, like the SDK, and wait for it
    install = { cb -> thread(name = "fake-GMA(BG)") { cb.onShowed(); cb.onDismissed() }.join() },
    onAppeared = { onMain = Looper.myLooper() == Looper.getMainLooper() },
    // then drain the posts queued on the main looper
    shadowOf(Looper.getMainLooper()).idle()
    assertEquals(true, onMain)  // false once mainHandler.post is removed
  4. Run the debug build against test ad units up to the point where an interstitial really appears. Our journey has six steps: first launch, cold start, Home key and back after 35 s, open the photo picker and cancel after 35 s, Start over after the first job, Start over after the second job. Then check that adb logcat -b crash has no FATAL entries.
  5. Log canRequestAds(), the consent status, the consent round trip and SDK init time at process start, and set the splash wait and its budget from those numbers. These are the lines our app wrote (collected from different runs):
    GmaAdsAdapter: UMP at start: canRequestAds=false status=0 debugSettings=false
    GmaAdsAdapter: UMP consent update took 2246 ms
    GmaAdsAdapter: GMA initialized in 1295 ms, 4767 ms after process start
    AppOpen: app open skipped (cold): consent/SDK not ready in 2000 ms
    AppOpen: cold: entitlement answered in 4139 ms

No test guards the main-looper post yet

As of the repository on November 7, 2026, what protects this fix is a comment in GmaFullScreen.kt and one sentence in the ad design document. Deleting mainHandler.post would leave every unit test green, so the test in step 3 is the next thing to add. Tuning the cold-start budget from real-device logs is still to do, and whether to shorten the wait for the Play purchase check is waiting on an operator decision.

FAQ

Which thread do GMA Next-Gen full-screen ad callbacks run on?

In our crash log it was one of the SDK's background threads, GMA(BG) 4. The Next-Gen guides we checked (interstitial, app open, banner, migration) don't say, so post any UI work from those callbacks to the main looper.

Why does "setCurrentState must be called on the main thread" crash the app?

Calling Navigation's popBackStack() off the main thread made LifecycleRegistry throw when it tried to change a screen's lifecycle. In this app, the call came from the ad-shown callback returning to HOME.

Is it normal for UMP canRequestAds() to be false at app start?

In our measurements it was false, with status UNKNOWN, at the start of every cold launch, even where consent is not required. It changed only after the consent update finished, about 2.3 s on the emulator.

This note is written from values SugarMount measured itself; an AI coding agent was used to draft it.

All AX notes