Skip to content

Fix: Harden crash recovery for foreground service errors#577

Merged
d4rken merged 1 commit into
mainfrom
fix/uncaught-handler-safety-net
May 3, 2026
Merged

Fix: Harden crash recovery for foreground service errors#577
d4rken merged 1 commit into
mainfrom
fix/uncaught-handler-safety-net

Conversation

@d4rken

@d4rken d4rken commented May 3, 2026

Copy link
Copy Markdown
Member

What changed

Reduce the chance of a secondary crash when the app's main thread hits the rare foreground-service startup timing error. The historical safety net that swallows the first such error stays — but it can no longer add another crash on top while logging or reporting.

Technical Context

  • The pasted ANR shows Android's own pre-handler (RuntimeInit.logUncaught) blowing up inside StringBuilder.append while serializing the exception. That platform path runs before our default handler — we cannot prevent it. We can only ensure our suppression path doesn't add to the problem.
  • Extracted the handler into CapodUncaughtExceptionHandler with seams (main-thread provider, looper invoker, reporter callback, exit) so the suppression / delegation / repeat logic is unit-testable.
  • Wrapped Bugs.report() logging and reporter notification in runCatching — a hostile throwable's toString()/message or a failing reporter no longer escapes.
  • Logging.logInternal now isolates per-logger failures (a broken logger can't take down the rest), and Throwable.asLog() falls back to a class-name + safe-message summary if printStackTrace itself throws.
  • One-shot suppression is preserved: the second timing exception delegates to the previous handler as before.

Wrap logging, reporting, and Looper resume calls so the foreground service timing exception suppression cannot itself trigger another crash. Extract handler into a dedicated class with seams for unit tests.
@d4rken d4rken merged commit 82dc88f into main May 3, 2026
11 checks passed
@d4rken d4rken deleted the fix/uncaught-handler-safety-net branch May 3, 2026 10:41
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.

1 participant