tinygo-org / bluetooth

Cross-platform Bluetooth API for Go and TinyGo. Supports Linux, macOS, Windows, and bare metal using Nordic SoftDevice or HCI
https://tinygo.org
Other
740 stars 136 forks source link

adapter.Scan() can't recover from panic on linux #288

Open jasday opened 2 months ago

jasday commented 2 months ago

In gap_linux.go, you cannot recover from a panic made within the callback to adapter.Scan().

If you attempt to recover from a panic, and re-call Scan() after the recovery, a.adapter.Call("org.bluez.Adapter1.StartDiscovery", 0).Err returns an Operation already in progress error.

It looks like cancelChan was meant to be used to call a.adapter.Call("org.bluez.Adapter1.StopDiscovery", 0), but if the callback panics, the channel is never consumed, so the call to stop the discovery is never sent. This leads to the above error when attempting to rescan.

jasday commented 2 months ago

I think the solution is as follows, but would like some input from maintainers before putting up a pr.

smatton commented 1 month ago

I think I am seeing something related to this too, where sometimes I get the same "Operation already in progress" error and I can't seem to get out of that error state without restarting the bluetooth service or my application.