Repository navigation
fix(ble): keep the ring client process-scoped across Activity recreation (#113) - #115
Merged
Merged
Conversation
…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.
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 Fixed in
Left as is:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #113
What was happening
PulseLoopAppcreatedRingBLEClientand the other app-lifetime services withremember { … }, so they lived only as long as the composition.MainActivitydeclares noandroid: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:processConnectionOwner, the single process-wide connection slot.The new client could not claim the slot. It logged
Connection skipped: another PulseLoop BLE client owns GATTand 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: bothdestroy()infinally, which releases the slot.The same rebuild also left a second
EventPersistenceSubscriber,RingSyncCoordinator,BatteryAlertMonitor,CoachSummaryCoordinatorandCoachNotificationDataTriggersubscribed to the event bus. Theirstart()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.PulseLoopAppreads them fromAppServicesinstead ofremembering its own.claimStartup()). That covers the callback wiring, thestart()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.ON_STARTnow runsreconnectIfNeeded(). 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 newStartupGateTest, which checks the startup gate opens exactly once, including under concurrent claims.Device check: connect the ring, then with the app open switch the system dark theme on and off (or change font size).
Connection skipped: another PulseLoop BLE client owns GATT.🤖 Generated with Claude Code