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:
co-authored by
Claude Sonnet 4.6
parent
31bb5d86c6
commit
ea4118ffcd
+11
-2
@@ -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
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user