mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-20 14:27:42 +00:00
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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user