fix(ble): resolve BLE connection hangs on macOS without --debug flag #893
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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