Skip to content

Fix Sentry 1GDC/1GEB/1GAF/1G8Z: move Reader DB writes off main thread#22861

Merged
nbradbury merged 6 commits into
trunkfrom
fix/sentry-1gdc-reader-volley-db-writes
May 18, 2026
Merged

Fix Sentry 1GDC/1GEB/1GAF/1G8Z: move Reader DB writes off main thread#22861
nbradbury merged 6 commits into
trunkfrom
fix/sentry-1gdc-reader-volley-db-writes

Conversation

@nbradbury
Copy link
Copy Markdown
Contributor

@nbradbury nbradbury commented May 13, 2026

Summary

Fixes a cluster of Reader ANRs where Volley response callbacks ran a SQLite compileStatement + execute on the main thread:

Volley's ResponseDeliveryRunnable runs callbacks on the main thread. When the SQLite connection pool is busy with concurrent writes (Reader sync), parking on acquireConnection from the main thread tipped these calls over the 5s ANR threshold.

Changes

  • ReaderPostRepository.kt — both setTagLastUpdated(tag) call sites (requestPostsWithTag, requestPostsForLatestStream) now run on a background Thread, matching the existing pattern in handleUpdatePostsResponse.
  • ReaderBlogActions.javahandleUpdateBlogInfoResponse now performs addOrUpdateBlog on a background Thread and posts UpdateBlogInfoListener.onResult back on the main thread via Handler(Looper.getMainLooper()). The two existing callers (ReaderSiteHeaderView, ReaderFetchSiteUseCase) expect the result on main — ReaderSiteHeaderView calls isAttachedToWindow() and updates views in the callback.

Test plan

  • Open the Reader, refresh the feed and verify posts load
  • Pull-to-refresh repeatedly on a tag stream while scrolling — no UI jank or ANR.

… off main

Volley's ResponseDeliveryRunnable runs callbacks on the main thread, and
two Reader paths then performed a SQLite compileStatement + execute on
that callback. On a busy SQLite connection pool this exceeded the ANR
budget.

ReaderTagTable.setTagLastUpdated (called from
ReaderPostRepository.requestPostsWithTag and requestPostsForLatestStream)
now runs on a background Thread.

ReaderBlogActions.handleUpdateBlogInfoResponse now performs
ReaderBlogTable.addOrUpdateBlog on a background Thread and posts
UpdateBlogInfoListener.onResult back on the main thread, since the
existing callers (ReaderSiteHeaderView, ReaderFetchSiteUseCase) expect
the result on main.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dangermattic
Copy link
Copy Markdown
Collaborator

dangermattic commented May 13, 2026

1 Warning
⚠️ PR is not assigned to a milestone.

Generated by 🚫 Danger

@wpmobilebot
Copy link
Copy Markdown
Contributor

wpmobilebot commented May 13, 2026

App Icon📲 You can test the changes from this Pull Request in WordPress Android by scanning the QR code below to install the corresponding build.

App NameWordPress Android
Build TypeDebug
Versionpr22861-ce271d0
Build Number1488
Application IDorg.wordpress.android.prealpha
Commitce271d0
Installation URL443ptdea4jim8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot
Copy link
Copy Markdown
Contributor

wpmobilebot commented May 13, 2026

App Icon📲 You can test the changes from this Pull Request in Jetpack Android by scanning the QR code below to install the corresponding build.

App NameJetpack Android
Build TypeDebug
Versionpr22861-ce271d0
Build Number1488
Application IDcom.jetpack.android.prealpha
Commitce271d0
Installation URL2opkgolrbo3j0
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.22%. Comparing base (75bb1f2) to head (ce271d0).
⚠️ Report is 1 commits behind head on trunk.

Files with missing lines Patch % Lines
...s/android/ui/reader/actions/ReaderBlogActions.java 0.00% 7 Missing ⚠️
...droid/ui/reader/repository/ReaderPostRepository.kt 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk   #22861      +/-   ##
==========================================
- Coverage   37.22%   37.22%   -0.01%     
==========================================
  Files        2318     2318              
  Lines      124561   124564       +3     
  Branches    16917    16917              
==========================================
  Hits        46371    46371              
- Misses      74440    74443       +3     
  Partials     3750     3750              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nbradbury nbradbury marked this pull request as ready for review May 15, 2026 16:25
@nbradbury nbradbury requested a review from adalpari May 15, 2026 16:25
updateAction == ReaderPostServiceStarter.UpdateAction.REQUEST_REFRESH
) {
ReaderTagTable.setTagLastUpdated(tag)
Thread { ReaderTagTable.setTagLastUpdated(tag) }.start()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Kotlin file, why not use coroutines instead of threads?

}
final ReaderBlog blogInfo = ReaderBlog.fromJson(jsonObject);
// Move the INSERT OR REPLACE off the main thread; callers expect onResult on main.
new Thread(() -> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ I'm not sure using raw threads is the best approach here. I asked Claude about using a handler and a Loop:

HandlerThread + Handler — a long-lived background thread with its own Looper. Arguably better here because all these DB writes serialize through SQLite anyway, so a single HandlerThread would naturally queue them and avoid spawning N short-lived threads on response bursts. The cost is managing its lifecycle (or making it static and never tearing it down).

nbradbury and others added 2 commits May 18, 2026 08:34
Both call sites already have applicationScope + ioDispatcher injected, and
the newer functions in this file use the same coroutine pattern.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces the per-response new Thread() in handleUpdateBlogInfoResponse with
a static single-thread ExecutorService + static main-thread Handler. DB
writes now serialize through one shared worker, avoiding thread churn on
response bursts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@nbradbury
Copy link
Copy Markdown
Contributor Author

This is a Kotlin file, why not use coroutines instead of threads?
I'm not sure using raw threads is the best approach here.

You're correct on both counts. I've updated this PR with your suggestions.

Copy link
Copy Markdown
Contributor

@adalpari adalpari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes, LGTM!

@nbradbury nbradbury enabled auto-merge (squash) May 18, 2026 14:52
@nbradbury nbradbury merged commit cb1714a into trunk May 18, 2026
18 of 22 checks passed
@nbradbury nbradbury deleted the fix/sentry-1gdc-reader-volley-db-writes branch May 18, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants