Skip to content

Fix: Reduce foreground service timeout crash on cold start#451

Merged
d4rken merged 1 commit intomainfrom
fix/foreground-service-timeout
Mar 20, 2026
Merged

Fix: Reduce foreground service timeout crash on cold start#451
d4rken merged 1 commit intomainfrom
fix/foreground-service-timeout

Conversation

@d4rken
Copy link
Copy Markdown
Member

@d4rken d4rken commented Mar 18, 2026

What changed

Reduced the chance of a crash that could happen when the app starts in the background after a Bluetooth event. The monitoring service could fail to start quickly enough on devices under heavy load, causing a system-level crash.

Technical Context

  • App.onCreate() called startForegroundService() eagerly, starting the system's 10-second foreground timeout clock before the BroadcastReceiver even ran. This meant the timeout window included both the remainder of App.onCreate() and the full BluetoothEventReceiver.onReceive() execution — time the service couldn't use to call startForeground().
  • The call was redundant: BluetoothEventReceiver, BootCompletedReceiver, and OverviewViewModel all independently start the monitoring service when needed.
  • Removing it shifts the timeout start to the actual trigger point (the receiver), shrinking the window to just the service creation overhead.
  • Verified against AOSP ActiveServices.java that the system already deduplicates redundant startForegroundService() calls and skips timeouts for already-foreground services — no app-level deduplication needed.

Calling startMonitor() in App.onCreate() started the foreground service timeout

clock before the BroadcastReceiver even ran, consuming timeout budget

with post-init work and receiver processing. The service is already

started by BluetoothEventReceiver, BootCompletedReceiver, and

OverviewViewModel, making this call redundant.
@d4rken d4rken added the bug Something isn't working label Mar 18, 2026
@d4rken d4rken merged commit 992cb5e into main Mar 20, 2026
9 checks passed
@d4rken d4rken deleted the fix/foreground-service-timeout branch March 20, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant