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.
This commit is contained in:
Cobra
2026-04-15 00:30:32 -04:00
parent f28b11801a
commit a9d43ce4d3
+4
View File
@@ -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