Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jasday opened this issue Aug 20, 2024 · 2 comments
Open

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

jasday opened this issue Aug 20, 2024 · 2 comments

Comments

@jasday
Copy link

jasday commented Aug 20, 2024

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
Copy link
Author

jasday commented Aug 20, 2024

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

  • Add a deferred call to a.adapter.Call("org.bluez.Adapter1.StopDiscovery", 0) to cleanup after function exit
  • Add a check before calling StartDiscovery to ensure that the device hasn't already started - this should prevent the above error:
// Instruct BlueZ to start discovering if it isn't already.
if disc, err := a.adapter.GetProperty("org.bluez.Adapter1.Discovering"); disc.Value().(bool) == false && err == nil {
	err = a.adapter.Call("org.bluez.Adapter1.StartDiscovery", 0).Err
	if err != nil {
		return err
	}
        // Add the defer a.adapter.Call("org.bluez.Adapter1.StopDiscovery", 0) here
} else if err != nil {
	return err
}

@smatton
Copy link

smatton commented Sep 16, 2024

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.

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

No branches or pull requests

2 participants