CMM-2174: Move background-start work off the main thread - #23285
CMM-2174: Move background-start work off the main thread#23285nbradbury wants to merge 4 commits into
Conversation
Play Console names the main-thread frames behind the Sentry "Background ANR" bucket: WordPressDB open, WorkManager init, the Gravatar Quick Editor startup initializer (AndroidKeyStore/Tink in a ContentProvider), the BOOT_COMPLETED receiver, and analytics metadata DB reads. - Remove the Gravatar androidx.startup initializer from the manifest and initialize it on demand from MeFragment - Open WordPressDB lazily (synchronized getter) and warm it on IO - Initialize WorkManager on demand via Configuration.Provider; enqueue the periodic upload work off the main thread - Run NotificationsPendingDraftsReceiver work on a background thread with goAsync() - Move refreshMetadata, install/upgrade tracks and the notification state check into one ordered IO coroutine; move the media-sanitize site lookup into its worker thread - Drop the dead fatal_db_error launch check and its string
Generated by 🚫 Danger |
Project manifest changes for WordPressThe following changes in the --- ./build/reports/diff_manifest/WordPress/jetpackRelease/base_manifest.txt 2026-09-03 15:25:31.075445801 +0000
+++ ./build/reports/diff_manifest/WordPress/jetpackRelease/head_manifest.txt 2026-09-03 15:25:49.996758768 +0000
@@ -1316,9 +1316,6 @@
android:authorities="com.jetpack.android.androidx-startup"
android:exported="false" >
<meta-data
- android:name="com.gravatar.quickeditor.initializer.QuickEditorContainerInitializer"
- android:value="androidx.startup" />
- <meta-data
android:name="androidx.emoji2.text.EmojiCompatInitializer"
android:value="androidx.startup" />
<meta-dataGo to https://buildkite.com/automattic/wordpress-android/builds/28500/canvas?sid=01a067dd-5c14-429c-b542-b190d191bf63, click on the |
Project manifest changes for WordPressThe following changes in the --- ./build/reports/diff_manifest/WordPress/wordpressRelease/base_manifest.txt 2026-09-03 15:25:26.623871474 +0000
+++ ./build/reports/diff_manifest/WordPress/wordpressRelease/head_manifest.txt 2026-09-03 15:25:45.034309749 +0000
@@ -1291,9 +1291,6 @@
android:authorities="org.wordpress.android.androidx-startup"
android:exported="false" >
<meta-data
- android:name="com.gravatar.quickeditor.initializer.QuickEditorContainerInitializer"
- android:value="androidx.startup" />
- <meta-data
android:name="androidx.emoji2.text.EmojiCompatInitializer"
android:value="androidx.startup" />
<meta-dataGo to https://buildkite.com/automattic/wordpress-android/builds/28500/canvas?sid=01a067dd-5c14-4b6c-98e5-cf59ac151c3e, click on the |
|
|
|
|
🤖 Build Failure AnalysisThis build has failures. Claude has analyzed them - check the build annotations for details. |
- Make WordPress.wpDB a lazy property; collapse the duplicate catch and remember a failure so a broken database isn't deleted on every access - Add a launchIo helper in AppInitializer so every background step is guarded the same way; use it for the media-sanitize step too - Keep install/upgrade tracking on the main thread so it precedes APPLICATION_OPENED; only the metadata refresh moves to IO - Catch Exception rather than Throwable in the boot receiver - Move the Gravatar initializer lookup into GravatarQuickEditorInitializer, warm it from MeFragment off the main thread, and initialize it before any of the library's exported activities is created
Lint's UnsafeProtectedBroadcastReceiver only looks at onReceive itself, so read the action there and pass it to the background handler.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## trunk #23285 +/- ##
==========================================
- Coverage 37.91% 37.91% -0.01%
==========================================
Files 2352 2353 +1
Lines 128795 128814 +19
Branches 17898 17897 -1
==========================================
+ Hits 48836 48837 +1
- Misses 75964 75982 +18
Partials 3995 3995 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|


TL;DR
We've had a long-standing issue with various ANRs at start-up caused by initializing processes on the main thread. This PR attempts to resolve most of the ANRs by moving these processes off the main thread:
BOOT_COMPLETEDDescription
Fixes CMM-2174 (Sentry JETPACK-ANDROID-PB9, "Background ANR").
That Sentry issue is an ANRv2 bucket built from the main thread's idle-looper stack, so it can't point at a cause. Play Console can: for 3 Aug–1 Sep the named main-thread frames on background process starts (WorkManager
SystemJobService,BOOT_COMPLETED, FCM) wereWordPressDB.<init>,AppInitializer.initWorkManager, the Gravatar Quick Editor'sandroidx.startupinitializer (AndroidKeyStore + Tink self-test inside a ContentProvider, beforeApplication.onCreate), and the pending-drafts receiver.MeFragment. The class is Kotlin-internal, hence the lookup by name;startup-runtime's consumer rules keep allInitializers under R8.WordPressDB:WordPress.wpDBis now a lazy synchronized getter (same delete-and-recreate fallback), warmed onDispatchers.IO. The deadisWpDBInitializedcheck inWPLaunchActivityand itsfatal_db_errorstring are removed.Configuration.ProvideronWordPress; the periodic upload enqueue runs off main.InitializationRulejoins that job soUploadWorkerTestcan't race the test WorkManager swap.NotificationsPendingDraftsReceiver:goAsync()+ background thread.refreshMetadata, install/upgrade tracks and the notification-state check run in one ordered IO coroutine (Tracks attributes events from state the refresh sets). The media-sanitize site lookup moves into its worker thread.Every new
appScope.launchis try/caught because that scope has a plainJob.Success metric is the Play ANR buckets above, not the Sentry issue, which will reopen regardless. The
SystemJobServicebucket should shrink, not vanish: WorkManager still initializes on main inside its own service on job-triggered starts.Not addressed here: Sentry init, fluxc
AccountStoreconstructor DB read, fluxc Volley queue creation,EncryptedLoggingconstruction.Testing instructions
Gravatar editor:
Note: If you get an "Oops" message here, it's unrelated to this PR - it's a known issue that has been reported to the Gravatar team.
Legacy DB:
Boot receiver:
adb logcat -s WordPress-NOTIFSshows "entering Pending Drafts Receiver from BOOT_COMPLETED"WorkManager: