manolofdez / AsyncBluetooth

A small library that adds concurrency to CoreBluetooth APIs.
MIT License
160 stars 30 forks source link

Crash when switching off bluetooth while scan is ongoing #35

Closed dev-pac closed 11 months ago

dev-pac commented 11 months ago

Version: from 1.7.0 (not tested earlier)

Steps to reproduce:

result:

Thread 5: Fatal error: SWIFT TASK CONTINUATION MISUSE: scanForPeripherals(withServices:options:) tried to resume its continuation more than once, throwing operationCancelled!

in CentralManaager().scanForPeripherals(...) in the catch block, line 87

expected:

dev-pac commented 11 months ago

Modification: working correctly in 1.4.1

manolofdez commented 11 months ago

Aha! I know why this is happening!! scanForPeripherals will resume with an async stream, but the enqueue call stays awaiting until the stream completes. Turning off Bluetooth does not terminate the stream. So you're probably calling cancelAllOperations when the bluetooth is turned off, which tries to send an operationCancelled to the enqueued operations. But since the scanForPeripherals continuation has already been used, the app crashes.

I'm going to want to re-think the strategy here, but I have a quick workaround for this. Stay tuned....

dev-pac commented 11 months ago

Cool; thanks