Use _stop_event consistently across all threads as the way to signal that it should stop. Add thread joins with timeouts to allow the program to exit cleanly

This commit is contained in:
Ian Renton
2026-09-18 09:21:38 +01:00
parent d79c8f72c8
commit 29d8654234
28 changed files with 239 additions and 126 deletions
+8 -1
View File
@@ -1,3 +1,4 @@
import logging
import os
from datetime import datetime
from threading import Event, Thread
@@ -14,6 +15,8 @@ from core.prometheus_metrics_handler import alerts_gauge, memory_use_gauge, spot
from telnetserver.telnetserver import TELNET_SERVER
from webserver.webserver import WEB_SERVER
logger = logging.getLogger(__name__)
class StatusReporter:
"""Provides a timed update of the application's status data."""
@@ -33,13 +36,17 @@ class StatusReporter:
def start(self):
"""Start the reporter thread"""
self._thread = Thread(target=self._run, name="StatusReporter")
self._thread = Thread(target=self._run, name="StatusReporter", daemon=True)
self._thread.start()
def stop(self):
"""Stop any threads and prepare for application shutdown"""
self._stop_event.set()
if self._thread:
self._thread.join(timeout=15)
if self._thread.is_alive():
logger.warning("Status reporter worker thread did not exit on time and will be killed.")
def _run(self):
"""Thread entry point: report immediately on startup, then on each interval until stopped"""