Skip to content

fix(app): scope device connection teardown to the device it targets - #13197

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
singhharsh1708:feat/app-multi-device-connections
Sep 10, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
singhharsh1708:feat/app-multi-device-connections

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

forgetDevice(deviceId) disconnects and disposes whichever connection happens to be open rather than the one it was asked to forget, because DeviceService keeps a single connection field and never checks the id against it. Today that is invisible, since there is only ever one connection. It stops being invisible the moment a second device exists.

Fixing it properly means the service has to know which connection belongs to which device, so connections are keyed by device id and teardown is scoped to the device being connected, disconnected, forgotten or stopped. stop() now tears down every connection instead of leaving them open, which the single field did too.

ensureConnection keeps its signature and all 63 callers already pass a device id, so they did not move. The two places that read the old single-connection field are migrated rather than aliased: _bleDisconnectDevice already had the device it was disconnecting, and the speech profile page resolves the codec through the device the page is recording from. Stored-device lookup reads btDevices instead of the single btDevice, so a device other than the last paired one can still be found on a background reconnect.

I am not claiming #2954 with this. You said there in April that OmiGlass records audio already and the feature was not needed, so this is the connection-ownership bug and its guard, nothing more. Capture still records one device.

Verified by running, from app/:

$ flutter test test/unit/device_service_connections_test.dart
00:00 +6: All tests passed!

$ bash test.sh
02:18 +1855 ~5: All tests passed!

$ bash scripts/analyze_ratchet.sh
Analyzer ratchet passed.

$ make preflight          (repo root)
PR preflight passed: 14 checks in 12.83s.

The six tests drive the registry through an injectable connection builder, the same dependency-injection shape DeviceProvider already uses for its loaders: a second device connecting leaves the first connected, an already connected device is reused, and disconnecting, forgetting or stopping tears down only what it should. I have no omi device here, so none of this ran over real BLE.

Failure-Class: none

Review in cubic

Connections are keyed by device id and teardown is scoped to the device
being connected, disconnected or forgotten, so connecting a second
device no longer drops the first. forgetDevice previously tore down
whichever connection was open rather than the one it was asked to
forget.
@singhharsh1708

singhharsh1708 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Test output for the behaviour claims above, since there is no UI to screenshot here:

$ flutter test test/unit/device_service_connections_test.dart
00:00 +0: connecting a second device leaves the first connected
00:00 +1: an already connected device is reused rather than reconnected
00:00 +2: disconnecting one device leaves the other alone
00:00 +3: forgetting one device does not tear down another
00:00 +4: without force a device that has never connected is not connected
00:00 +5: All tests passed!

Correcting my own line above: I first wrote that two of these fail on current main, which is not something I ran. They cannot run against main unchanged, because the service API differs there (no connectionFor, and disconnectDevice took no argument). The two behaviours that actually change are the first and the fourth.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread app/lib/services/devices.dart Resolved
Comment thread app/lib/pages/speech_profile/page.dart Resolved
… recording device

stop() left every connection open, and the speech profile page resolved
its codec through the persisted device id while the page itself records
the provider's device; those differ once more than one device exists.
@singhharsh1708 singhharsh1708 changed the title feat(app): let DeviceService hold a connection per device fix(app): scope device connection teardown to the device it targets Sep 8, 2026
@singhharsh1708

Copy link
Copy Markdown
Contributor Author

Both fixed in 61655d6. stop() now tears down every connection instead of leaving them open, and the speech profile page resolves its codec through the device it is actually recording from rather than the persisted id. Six tests now, and I retitled this: I am not claiming #2954, since you said in April OmiGlass records audio already and the feature was not needed. What is left is the forgetDevice ownership bug and its guard.

@Git-on-my-level

Copy link
Copy Markdown
Member

Verified the bug this fixes: on main, forgetDevice(deviceId) disconnects and disposes whichever connection happens to sit in the single _connection field without ever comparing the id, and disconnectDevice() took no id at all. Keying connections by device id is the right shape for the fix, and the migration is complete — a repo-wide search at this head finds no remaining caller of the old no-arg disconnectDevice() or the old .device.connection accessor.

Per file:

app/lib/services/devices.dart

  • The _connections map plus _teardownConnection(deviceId) correctly scopes connect/disconnect/forget/stop to the targeted device, and teardown now has per-step try/catch — an improvement over the old forgetDevice, where a throwing disconnect() skipped transport.dispose() entirely.
  • ensureConnection keeps its per-device semantics under the same _mutex: connected -> return it; disconnected transport without force -> null so native auto-reconnect survives; force -> connect. Two visible behavior changes are worth maintainer eyes:
    1. On a forced reconnect of a device with a live-but-disconnected transport, main disconnected and disposed the old transport before building a new one; this head reuses the existing connection object and re-connect()s it. Defensible and locked in by the "reused rather than reconnected" test — just confirm it matches what the native BLE layer expects.
    2. stop() now disconnects and disposes every connection instead of silently dropping the single field without cleanup. That is a leak fix, but it changes shutdown/backgrounding behavior for every device type.
  • _connectionBuilder defaults to DeviceConnectionFactory.create and the only production instantiation is DeviceService() in services.dart, so the DI seam is test-only in effect — no production behavior change from it.

app/lib/providers/device_provider.dart — _bleDisconnectDevice now passes btDevice.id, and it is the only production caller of disconnectDevice, so the signature change is fully migrated.

app/lib/pages/speech_profile/page.dart — _getAudioCodec(deviceId) reads connectionFor(currentDevice.id) from the same provider.device the page records from, preserving the null -> BleAudioCodec.pcm8 fallback. This closes a latent wrong-device codec read without changing single-device behavior.

app/lib/services/services.dart — deinit() now awaits _device.stop(). It was already an async fire-and-forget method, so callers are unchanged; teardown is now actually awaited inside it.

app/test/unit/device_service_connections_test.dart — six focused tests driving the registry through the injected builder: second-device connect leaves the first connected, a connected device is reused, disconnect/forget of one leaves the other untouched (transport dispose asserted separately), stop() tears down everything, and no-force never connects. Pigeon-channel and SharedPreferences mocks only, no I/O — matches the repo's unit-test conventions.

Scope check: this is the connection-ownership bug and its guard, not multi-device capture — the description explicitly does not claim #2954 and capture still records one device. The registry is the minimal foundation that makes the guard testable, so I don't think a split is warranted.

CI note: no check runs are configured on this head, so the locally posted flutter test / test.sh / analyzer-ratchet output is unverified by automation; I reviewed statically and did not execute the suite.

Nice work @singhharsh1708 — the bug writeup in the description is precise, the follow-up comment correcting which tests fail on main is honest, and the tests target exactly the regression being fixed. Positive signal from me. Since every device type's connection lifecycle flows through DeviceService, and the two semantic changes above (forced-reconnect transport reuse, stop() teardown) can only be confirmed on real hardware, leaving the merge call to the maintainers.

Automated maintainer-review feedback on behalf of the Omi maintainers.


by AI on behalf of David — human maintainer sign-off requested for the two connection-lifecycle behavior changes noted above before merge.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution needs-maintainer-review Needs a human maintainer to sign off before merge flutter flutter work labels Sep 8, 2026
@singhharsh1708

Copy link
Copy Markdown
Contributor Author

On the forced-reconnect point, I don't think that one holds. _connectToDevice still calls _teardownConnection(id) first, which disconnects and disposes the transport before a new connection is built, which is what the old inline block did. The reuse is only the early return when the device is already connected, and main returned its single connection in that case too. stop() is a real behaviour change though, and it needs a device to confirm.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mobile bug fix (device-id-scoped connection teardown), confidence ~4/5 (clear root cause, fix(app) title, scoped diff, regression tests) but zero CI checks have actually run on this fork PR (statusCheckRollup empty) -- holding at approve until CI executes rather than merging on self-reported test output alone.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: forgetDevice/disconnect teardown now scoped per-device-id instead of a single shared connection field — real defect once a second device exists, well-scoped diff (271 lines), 5 new regression tests. Confidence 5/5, merges under mobile bug-fix confidence gate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work needs-maintainer-review Needs a human maintainer to sign off before merge positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants