fix(ble): resolve BLE connection hangs on macOS without --debug flag#893
Conversation
This fixes two issues that caused BLE connections to hang on macOS
when not using the --debug flag:
1. Race condition in BLEClient event loop initialization
- The event loop thread was started but asyncio operations were
submitted before the loop was actually running
- Added threading.Event synchronization to ensure the event loop
is running before any operations are submitted
- The ready signal is sent from within the loop via call_soon()
to guarantee the loop is truly active
2. CoreBluetooth callback delivery on macOS
- On macOS, CoreBluetooth requires occasional I/O operations for
callbacks to be properly delivered to the main thread
- Without --debug, no I/O was happening, causing callbacks to
never be processed and operations to hang indefinitely
- Added sys.stdout.flush() call before waiting for async results
to trigger the necessary I/O
The --debug flag masked these issues because:
- Debug logging introduces timing delays that let the event loop start
- Logger I/O triggers the necessary callback delivery mechanism
Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
|
|
|
Oh, are there any people who really aren't using Meshtastic without debugging? |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #893 +/- ##
==========================================
- Coverage 59.93% 59.82% -0.11%
==========================================
Files 24 24
Lines 4320 4329 +9
==========================================
+ Hits 2589 2590 +1
- Misses 1731 1739 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
This looks reasonable to me. The stdout flush presumably isn't necessary on non-osx platforms but I don't think it'll hurt to have it there either. |
Summary
Fix BLE connections hanging indefinitely on macOS when not using the
--debugflag.Problem
On macOS, BLE connections would hang during scanning or connecting unless the
--debugflag was specified. This made the CLI unusable for normal BLE operations without verbose output.Root Cause
Two issues were identified:
Event loop race condition: The asyncio event loop thread was started, but operations could be submitted before the loop was actually running, causing deadlocks.
CoreBluetooth callback delivery: On macOS, CoreBluetooth requires the main thread to perform occasional I/O for callbacks to be delivered. Without
--debug, no I/O occurred, so callbacks were never processed.The
--debugflag masked both issues by introducing timing delays and I/O operations through logging.Solution
Added
threading.Eventsynchronization to ensure the event loop is running before any async operations are submitted.Added a
sys.stdout.flush()call before waiting for async results, which triggers the necessary I/O for CoreBluetooth callback delivery.Testing
Tested on macOS 26.2 (Tahoe):
meshtastic --ble-scancompletes successfullymeshtastic --ble <device> --nodesconnects and retrieves node informationmeshtastic --ble <device> --infoworks as expected