fix: CrashHandler installs once and dispatches each crash once

Merged
JE
6d ago
+63-2

Description

Every PluviaApp boot wraps the previous default handler, so under Robolectric the chain grows by one per test. When the crash-file save fails, the catch also hands the crash on before uncaughtException does, so each failing link doubles the calls below it. device-info dies on every Robolectric boot, and stale contexts fail getPackageInfo, so depending on timing one crash can queue millions of setPref writes and hang the fork in GC. Master gets lucky; #1570 doesn't.

initialize now unwraps an existing CrashHandler instead of wrapping it, and the catch no longer dispatches.

Recording

n/a -- test infrastructure / crash handling, nothing visible.

Type of Change

  • Bug fix
  • Performance / stability improvement
  • Compatibility improvements
  • Other (requires prior approval)

Test plan

  • Full legacy suite on master + this: same results as master (2962 tests)
  • Full legacy suite on #1570 + this: passes (4189 tests, 8.5 min), previously hung in GC every time.
  • On device, a forced crash still writes the crash file and reaches the system handler

Checklist

  • If I have access to #code-changes, I have discussed this change there and it has been green-lighted. If I do not have access, I have still provided clear context in this PR. If I skip both, I accept that this change may face delays in review, may not be reviewed at all, or may be closed.
  • This change aligns with the current project scope (core functionality, stability, or performance). If not, it has been explicitly approved beforehand.
  • I have attached a recording of the change.
  • I have read and agree to the contribution guidelines in CONTRIBUTING.md.

Summary by cubic

Fixes CrashHandler so it installs once and dispatches each crash once. Every PluviaApp boot previously wrapped the existing default uncaught exception handler, so the chain grew per boot under Robolectric, and when saving a crash file failed the catch block also forwarded the crash before uncaughtException did, doubling dispatch per link. Depending on timing, one crash could queue millions of writes and hang the test in GC.

initialize now unwraps an existing CrashHandler to reach the original default handler instead of wrapping it. The catch block no longer forwards the crash itself and instead logs the failed save via Timber. Adds tests covering repeated initialization and failed saves.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented repeated crash-handler initialization from adding duplicate handler chains.
    • Uncaught crashes are now passed to the default handler exactly once, including when saving a crash report fails. Report-saving errors are logged.
    • Crash reports are still saved when possible before the uncaught crash is passed to the default handler.