Fix silent watcher thread death with watchdog supervisor

Move time.sleep inside the try/except to catch BaseException-level
failures, and wrap the watcher thread in a supervisor that auto-restarts
it within 5s if it ever exits unexpectedly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
perco
2026-06-03 14:59:18 +02:00
co-authored by Claude Sonnet 4.6
parent 31bb5d86c6
commit ea4118ffcd
2 changed files with 13 additions and 3 deletions
+11 -2
View File
@@ -26,11 +26,20 @@ os.makedirs(os.path.join(ANNOTATIONS_DIR, "images"), exist_ok=True)
os.makedirs(os.path.join(ANNOTATIONS_DIR, "labels"), exist_ok=True) os.makedirs(os.path.join(ANNOTATIONS_DIR, "labels"), exist_ok=True)
def _watcher_supervisor():
while True:
t = threading.Thread(target=watcher.run_watcher, daemon=True, name="watcher")
t.start()
t.join()
logging.warning("Watcher thread exited unexpectedly — restarting in 5s")
time.sleep(5)
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
database.init_db() database.init_db()
t = threading.Thread(target=watcher.run_watcher, daemon=True) sup = threading.Thread(target=_watcher_supervisor, daemon=True, name="watcher-supervisor")
t.start() sup.start()
yield yield
+2 -1
View File
@@ -377,6 +377,7 @@ def run_watcher():
_last_event_time = now _last_event_time = now
log.info("Vehicle detected!") log.info("Vehicle detected!")
_process_event() _process_event()
time.sleep(POLL_INTERVAL)
except Exception as e: except Exception as e:
log.error(f"Watcher loop error: {e}", exc_info=True) log.error(f"Watcher loop error: {e}", exc_info=True)
time.sleep(POLL_INTERVAL) time.sleep(POLL_INTERVAL)