Skip to content

fix(ble): keep the ring client process-scoped across Activity recreation (#113) - #115

Merged
foureight84 merged 2 commits into
mainfrom
fix/113-process-scoped-ble
Oct 14, 2026
Merged

foureight84 merged 2 commits into
mainfrom
fix/113-process-scoped-ble

Conversation

@foureight84

Copy link
Copy Markdown
Owner

Fixes #113

What was happening

PulseLoopApp created RingBLEClient and the other app-lifetime services with remember { … }, so they lived only as long as the composition. MainActivity declares no android:configChanges, so any configuration change rebuilds the Activity and that composition with it. Examples are a dark-theme switch (Pixel's Bedtime mode does one overnight), a locale or font-size change, or a rotation.

Each rebuild made a second RingBLEClient. The first was never destroyed and kept both:

  • the ring's GATT link, which is why Android still reported the ring connected, and
  • processConnectionOwner, the single process-wide connection slot.

The new client could not claim the slot. It logged Connection skipped: another PulseLoop BLE client owns GATT and retried every second, and showed Disconnected. Connect and "Reconnect last ring" went through the same new client, so they did nothing either. That matches the reporter's log, from a process that had been up since Oct 7.

The background clients (RingSyncWorker, CoachNotificationSlotRunner) were checked and aren't the cause: both destroy() in finally, which releases the slot.

The same rebuild also left a second EventPersistenceSubscriber, RingSyncCoordinator, BatteryAlertMonitor, CoachSummaryCoordinator and CoachNotificationDataTrigger subscribed to the event bus. Their start() is idempotent per instance, but each rebuild made new instances, so every frame was persisted and handled twice.

Change

  • AppServices (new): one set of those services per process, built from the application context, so nothing pins a destroyed Activity.
  • PulseLoopApp reads them from AppServices instead of remembering its own.
  • The one-shot startup runs once per process (claimStartup()). That covers the callback wiring, the start()s, the stale-state reset and the auto-reconnect. Re-running the stale-state reset after a rebuild would write DISCONNECTED over a live link.
  • A recreated Activity's first ON_START now runs reconnectIfNeeded(). Previously it was always skipped as "handled by startup", which a rebuilt Activity no longer runs.
  • AGENTS.md: new section on why these must stay process-scoped.

ViewModels and the coach orchestrator are still created per composition. They don't hold the ring, so they're out of scope here.

Tests

  • ./gradlew :app:testDebugUnitTest: 1483 tests, 0 failures. That includes the new StartupGateTest, which checks the startup gate opens exactly once, including under concurrent claims.
  • Not verified on a device. No emulator was available, and recreation can't be exercised without Robolectric or an instrumented test. The diagnosis matches the code and the log, but the recreation itself wasn't captured.

Device check: connect the ring, then with the app open switch the system dark theme on and off (or change font size).

  • Before this fix: the app shows Disconnected and logcat repeats Connection skipped: another PulseLoop BLE client owns GATT.
  • After it: the ring stays Connected, there's no loop, and Measure works.

🤖 Generated with Claude Code

…ion (#113)

PulseLoopApp remembered RingBLEClient and the other app-lifetime services in its composition,
which is rebuilt with the Activity on any configuration change (MainActivity handles none). Each
rebuild made a second client while the first, never destroyed, kept the GATT link and the
process-wide connection slot: the new one retried "Connection skipped: another PulseLoop BLE
client owns GATT" every second and showed Disconnected over a connected ring. The rebuild also
started duplicate persistence and coordinator subscribers on the event bus.

The services now live in AppServices, built once per process from the application context. The
one-shot startup (callback wiring, start()s, stale-state reset, auto-reconnect) runs for the
process's first composition only, and a recreated Activity's first ON_START reconnects instead of
being skipped.
The startup gate is claimed during composition, but the work ran in a LaunchedEffect: a rotation or theme change in the first moments of a cold start could cancel it partway, and with the gate already claimed nothing would finish it (stale-state reset, auto-reconnect, worker schedule). It now runs in AppServices.startupScope.
@foureight84

Copy link
Copy Markdown
Owner Author

Independent review (fresh-context reviewer, read-only): no blocking issues. It confirmed that the diagnosis fits the log, and that the only foreground RingBLEClient is now the singleton in AppServices.

Fixed in 9dbbf9d:

  • The one-shot startup claimed its gate during composition but ran in a cancellable LaunchedEffect. A rotation or theme change in the first moments of a cold start could cut it short, and nothing would ever finish it: the stale-state reset, auto-reconnect and worker schedule would be skipped. It now runs in AppServices.startupScope.

Left as is:

  • The 45 s background-disconnect timer still lives in the composition. If the Activity is destroyed during the grace period, the link stays up until the next foreground. That's not a regression (the leaked client behaved the same way). With the client now process-scoped, ProcessLifecycleOwner would be the natural owner for it in a follow-up.
  • isFirstStart is seeded per lifecycleOwner. With a single Activity this is theoretical.

@foureight84
foureight84 merged commit b2885a0 into main Oct 14, 2026
2 checks passed
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.

Ring shows Disconnected and Connect does nothing while another internal BLE client still holds the ring

1 participant