From a9d43ce4d3df948b91afe579ea61f8ba04e5a5e1 Mon Sep 17 00:00:00 2001 From: Cobra Date: Wed, 15 Apr 2026 00:30:32 -0400 Subject: [PATCH] Suppress BlueZ teardown race on SIGTERM in ble_alerter When systemd sends SIGTERM, asyncio cancels the scan task before BleakScanner.__aexit__ runs. BlueZ already stopped discovery by then, so stop() throws 'No discovery started'. This was crashing the service and triggering a 22-restart loop before systemd settled. Catch the specific error string and treat it as a debug-level no-op. --- net_alerter/ble_alerter.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/net_alerter/ble_alerter.py b/net_alerter/ble_alerter.py index d67c689..a84d5ef 100755 --- a/net_alerter/ble_alerter.py +++ b/net_alerter/ble_alerter.py @@ -352,6 +352,10 @@ async def scan_loop(): if "No Bluetooth adapters found" in str(e) or "No default Bluetooth adapter" in str(e): logging.warning("No Bluetooth adapter found — BLE scanning disabled.") shutdown_event.set() + elif "No discovery started" in str(e): + # BlueZ teardown race: SIGTERM cancelled the scanner before BleakScanner.__aexit__ + # ran stop(). BlueZ already stopped the scan, so this is harmless. + logging.debug(f"BlueZ teardown race on shutdown (ignored): {e}") else: logging.error(f"Failed to initialize BLE scanner: {e}") raise