Fix ANR when handling package add/remove broadcasts#678
Merged
Conversation
The package changed broadcast receiver ran Rule.clearCache() and its follow-up work directly on the main thread. clearCache() synchronizes on the application context, the same lock the command thread holds while building the rule list via getRules(), which in turn issues per-package PackageManager IPC. When those binder calls are slow, the command thread holds the lock for several seconds and the main thread blocks in clearCache(), stalling input dispatch and triggering an ANR. Move the receiver's work off the main thread with goAsync() plus the existing executor, so waiting on the lock no longer blocks the UI. Since both callers of notifyNewApplication() now run off the main thread, drop the nested goAsync()/Thread in the tracker-library check and run it inline, and replace the BroadcastReceiver parameter with a boolean flag. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018yc5s4aH1mWrYw4gpsHRrZ
kasnder
marked this pull request as ready for review
July 23, 2026 15:59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An ANR was reported in
DetailsActivity(Input dispatching timed out … Waited 5000ms). The captured thread dump shows a lock-contention stall, not a hang in the activity itself:Rule.clearCache()(Rule.java:201), called synchronously frompackageChangedReceiver.onReceive()(ServiceSinkhole.java:2827), waiting to lock theApplicationExmonitor.Rule.getRules()and is parked in a slowPackageManager.getPackageInfo()binder IPC (viaUtil.isSystem→Rule.<init>while building the rule list).Rule.getRules().Both
Rule.getRules()andRule.clearCache()synchronize oncontext.getApplicationContext().getRules()makes per-packagePackageManagerIPC while holding the lock; when that IPC is slow, the command thread keeps the monitor for several seconds. Because the package-changed broadcast receiver ranclearCache()directly on the main thread, the main thread blocked on that monitor, input dispatch stalled, and the system raised an ANR.Fix
ServiceSinkhole.javaonly:packageChangedReceivernow hands its work to the existingexecutorviagoAsync(), so waiting on the application-context monitor happens on a background thread and never blocks the UI. The receiver body was extracted intohandlePackageChanged(context, intent).notifyNewApplication()now run off the main thread, so the nestedgoAsync()/new Thread()inside the tracker-library check (checkTrackers) is no longer needed — it runs inline on the caller's background thread. CallinggoAsync()twice would have returnednullon the second call, so this also keeps the async handoff correct.notifyNewApplication(int, BroadcastReceiver)becamenotifyNewApplication(int, boolean)— theBroadcastReceiverargument existed only to feed that nestedgoAsync(). The notification-only caller inset()passesfalse; the install path passestrue.No behavioral change to what work happens on package install/removal — only which thread it runs on.
Testing
The full Gradle build could not run in this environment: the pinned Gradle 9.6.1 distribution and AGP 9.3.0 are blocked by the sandbox egress policy (GitHub returns 403 and nothing is cached for offline mode). The change is a localized, self-contained refactor within
ServiceSinkhole.java; a./gradlew :app:compileGithubDebugJavaWithJavacon a networked checkout is recommended before merge.🤖 Generated with Claude Code
https://claude.ai/code/session_018yc5s4aH1mWrYw4gpsHRrZ
Generated by Claude Code