IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.59k stars 491 forks source link

Hanging bug (with proposed fix) #390

Open brandyn opened 4 years ago

brandyn commented 4 years ago

If the connection is lost during (or just before?) a call to setSecurityLevel or setMTU, _getResp('stat') therein consumes the stat reply without noticing the disconnected state.

A subsequent call to disconnect() then sends 'disc' to the Helper which is ignored (because it's already disconnected), and then disconnect() blocks forever waiting for a reply it will never get (and just one step shy of calling _stopHelper()).

This is causing my app to hang when a device with a weak connection drops (undetected) early on, leading to a badstate exception on subsequent calls, and finally to a hang when my try:finally calls disconnect() attempting to clean things up. (Note if I didn't call disconnect, it could get called by del during GC which would be an even worse place to hang!)

I can see a few "easy" ways to fix it but all have possible side effects and I'm not familiar enough with the whole flow to know what's the best approach. Suggestions?

brandyn commented 4 years ago

Suggested fix:

Just comment out "self._getResp('stat')" in disconnect(). That will proceed directly to shutting down the helper; can't see the harm in leaving a last stat reply unread since its value is ignored anyway.

brandyn commented 4 years ago

(This fix works for me so far. Hasn't hung again, and has caused no new issues.)