Repository navigation
Conversation
bleak runs disconnected_callback on BLEClient's event loop thread, and the callback called close() inline. close() waits on operations that run on that same loop (writing the disconnect message, bleak's disconnect), so when the device drops the link itself, e.g. on a reboot, the loop deadlocks: connection.lost is never published and the client is never cleaned up. Run close() on its own thread instead. The re-entrancy guard from meshtastic#976 still covers the case where close() started the disconnect.
- _exit_handler defaults to None at class level: the annotation alone created no attribute, so a close() mid-__init__ (device dropping the link, now reachable) raised AttributeError and left the client half-closed - the reader treats any BleakError as a disconnect once bleak reports the link down (CoreBluetooth's errors don't say "Not connected"), and stops via continue rather than falling through to a possibly unbound read buffer - close() logs the unsendable disconnect message at debug, not as an ERROR pointing at bluetooth groups and pairing PINs, when the device already dropped the link - tests: connect() registers _on_disconnected; the reader stops quietly on a dropped link; close() works before the atexit handler is registered - type hints on the callback and test helper
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Follow-up to #976. That PR fixed
close()re-entering itself when we start the disconnect. This PR covers the case where the device drops the link, e.g. when it reboots.Cause
bleak runs
disconnected_callbackonBLEClient's event loop thread, and the callback calledself.close()inline. If the device disconnects first, that callback'sclose()is the first one, so the_closingguard doesn't stop it. It then waits on operations that run on that same loop:MeshInterface.close()writes the disconnect message, thenclient.disconnect(). The loop is blocked waiting on itself and deadlocks:meshtastic.connection.lostis never published;iface.clientis never cleared;BLEClientevent loop thread sits inwrite_gatt_char→async_await→future.result()forever.Fix
connect()passes_on_disconnectedas the callback, and it runsclose()on its own daemon thread. Whenclose()started the disconnect itself, the callback's thread hits the_closingguard and returns, as before._exit_handlerdefaults toNoneat class level. The annotation alone created no attribute, so aclose()while__init__was still running (the device dropping the link during the config download) raisedAttributeErrorand left the client half-closed.BleakErroras a disconnect once bleak reports the link down. CoreBluetooth's errors don't say "Not connected", so a read in flight at the drop used to surface as an uncaughtREAD_ERRORinBLEReceive.close()logs the unsendable disconnect message at debug. Before, every reboot loggedERROR Error closing mesh interface: Error writing BLE (are you in the 'bluetooth' user group? did you enter the pairing PIN on your computer?).Testing
New unit tests:
connect()registers_on_disconnected;close()works before the atexit handler is registered.The last two fail before this change.
Hardware: Heltec MeshPocket on firmware 2.7.26, connecting from macOS with bleak 1.1.1 on Python 3.14. A script connects, has the node reboot itself (local
reboot_seconds=2, no LoRa), waits 25 s and checks the state:connection.lostnot published, client still set, event loop thread stuck inwrite_gatt_char;connection.lostpublished, client cleared, event loop thread gone, no ERROR log;connection.lostand cleans up.pytest: 574 passed, 2 skipped. pylint and mypy report only import errors for the optionalriden,ppk2_api,parseandpyarrowpackages, which this env doesn't install.Linux is untested: on the one Linux machine available, master can't complete a BLE connection to the test node (the first
FROMRADIOread never returns), so the reboot path can't be reached there.This PR doesn't depend on #1026, the GATT-operation lock that fixes the related hang at connect. Either can merge first; the second needs a small rebase because both append tests to
test_ble_interface.py.Summary by CodeRabbit