Skip to content

Fix: Fix crash when monitor service fails to start in time#504

Merged
d4rken merged 1 commit intomainfrom
fix/foreground-service-timing-crash
Apr 14, 2026
Merged

Fix: Fix crash when monitor service fails to start in time#504
d4rken merged 1 commit intomainfrom
fix/foreground-service-timing-crash

Conversation

@d4rken
Copy link
Copy Markdown
Member

@d4rken d4rken commented Apr 14, 2026

What changed

Fixed a crash that could occur when the system kills the monitor service before it finishes starting up. This typically happens on cold starts triggered by Bluetooth events when the device is under load.

Technical Context

  • Root cause: Android throws ForegroundServiceDidNotStartInTimeException when startForeground() isn't called within ~5 seconds of startForegroundService(). The existing early promoteToForeground() call before Hilt DI mitigates most cases, but the system can still throw the exception on the main thread before MonitorService.onCreate() even runs — no in-service fix can prevent that.
  • Global handler in App.kt: Catches the exception, reports it via Bugs.report(), and revives the main Looper with Looper.loop() so the app continues functioning instead of crashing. A one-shot guard prevents recursive Looper nesting if the exception fires twice. This matches the approach from Revive main Looper after suppressing foreground service timing exception bluemusic#207.
  • Unguarded super.onCreate() in MonitorService: The foreground-denied branch called super.onCreate() (Hilt DI) without a try-catch, so a DI failure there would crash unhandled. Now wrapped in the same try-catch as the normal path.
  • onDestroy guard: Added injectionComplete flag so onDestroy() doesn't access @Inject lateinit var fields when DI never completed, preventing UninitializedPropertyAccessException.

@d4rken d4rken merged commit 29df906 into main Apr 14, 2026
10 checks passed
@d4rken d4rken deleted the fix/foreground-service-timing-crash branch April 14, 2026 09:24
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