Skip to content

fix: a cancelled or abandoned wait for a WiFi device no longer hangs forever - #146

Merged
gdoumen merged 3 commits into
mainfrom
feat/wifi-probe-before-announce
Nov 9, 2026
Merged

gdoumen merged 3 commits into
mainfrom
feat/wifi-probe-before-announce

Conversation

@gdoumen

@gdoumen gdoumen commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • DirectConnectInterface.waitForPeripheral()'s internal wait only ever settled on success. Cancelling it (pauseBackgroundActivity, or a caller giving up) and its own 5-minute lifetime both cleaned up internal state without ever resolving or rejecting the promise the caller was awaiting.
  • Any caller awaiting that promise — directly or indirectly, e.g. BleAdapter.stop() awaiting a still in-flight start() via its underlying promise — could be stuck for as long as the wait kept running, in practice indefinitely whenever the device stayed unreachable.
  • Found via real manual testing: a UI action that opens a device scan while a different device's connection attempt is still in flight hung indefinitely with no error, because the teardown of that in-flight attempt never completed.

What changed

  • waitForPeripheral()'s wait now rejects on cancel and on reaching its own lifetime, instead of only ever resolving.
  • Added cancelWait() to settle a specific device's pending wait without cancelling every other in-flight wait.
  • BleAdapter.start()'s task now calls cancelWait() via its existing onCancel hook when the start is stopped or times out, so a still-pending wait is interrupted immediately instead of left to its own, much longer, timeout. IBleInterface.cancelWait() is optional — an interface with no comparable internal wait doesn't need one.
  • runStop()'s wait on the previous start's underlying promise is now also bounded as a backstop, so stop() still returns even if some other promise in the chain genuinely never settles.

Test plan

  • New characterization tests added for each piece (direct-connect/base/interface.unit.test.ts, ble/fm/adapter.unit.test.ts), each manually verified to fail without its corresponding fix before being made to pass.
  • Full repo suite green: 1275 tests passed, 0 failed.
  • tsc --noEmit clean.

…forever

DirectConnectInterface.waitForPeripheral()'s internal wait only ever settled
on success: cancelling it (pauseBackgroundActivity, or a caller giving up)
and its own 5-minute lifetime both just cleaned up internal state without
resolving or rejecting the promise the caller was awaiting. Any caller
awaiting that promise - directly or indirectly, such as BleAdapter.stop()
awaiting a still in-flight start() via its underlying promise - was stuck
for as long as the wait happened to keep running, in practice indefinitely
whenever the device stayed unreachable.

- waitForPeripheral()'s wait now rejects on cancel and on its own lifetime
  being reached, instead of only ever resolving.
- a new cancelWait() lets a specific device's pending wait be settled
  without canceling every other interface's in-flight wait.
- BleAdapter.start()'s task now calls cancelWait() via the existing onCancel
  hook when the start is stopped or times out, so a still-pending wait is
  interrupted immediately rather than left to its own, much longer, timeout.
  IBleInterface.cancelWait() is optional - an interface with no comparable
  internal wait doesn't need one.
- runStop()'s wait on the previous start's underlying promise is now also
  bounded as a backstop, so stop() still returns even if some other promise
  in the chain genuinely never settles.
Checking a Promise-or-undefined field directly in a boolean conditional
reads like an accidentally-skipped await. Made the intent (is it set at all,
not whether it resolved truthy) explicit.
@sonarqubecloud

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

@gdoumen
gdoumen marked this pull request as ready for review November 9, 2026 12:21
@gdoumen
gdoumen merged commit b6f5121 into main Nov 9, 2026
6 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.

1 participant