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
+9 -2
View File
@@ -49,6 +49,7 @@ class TelnetServer:
self._running = False
self._clients = set()
self._loop = None
self._thread = None
self._shutdown_event = asyncio.Event()
def start(self, port=7373):
@@ -58,8 +59,10 @@ class TelnetServer:
# Start the telnet server. asyncio.run() needs a coroutine, and threading.Thread needs a plain callable, so
# hand Thread the bridge between the two directly rather than writing a one-line wrapper method for it.
t = threading.Thread(target=asyncio.run, args=(self._start_internal(),), name="TelnetServer", daemon=True)
t.start()
self._thread = threading.Thread(
target=asyncio.run, args=(self._start_internal(),), name="TelnetServer", daemon=True
)
self._thread.start()
logger.debug("Telnet server background thread spawned")
# Listen for new spots and alerts being added to the cache, so we can notify SSE clients immediately
@@ -135,6 +138,10 @@ class TelnetServer:
if self._loop and self._loop.is_running():
logger.debug("Stopping telnet server...")
self._loop.call_soon_threadsafe(self._shutdown_event.set)
if self._thread:
self._thread.join(timeout=15)
if self._thread.is_alive():
logger.warning("Telnet server background thread did not exit on time and will be killed.")
@property
def client_count(self) -> int: