Potential fix for an issue where the telnet client was reconnecting right at the same time we try to shut down spothole, causing the stop() method to close one telnet object but then a new one is created and read from anyway.

This commit is contained in:
Ian Renton
2026-09-19 07:55:12 +01:00
parent ab81c136cc
commit 59d5f61d90
10 changed files with 41 additions and 23 deletions
+13 -4
View File
@@ -1,7 +1,7 @@
import logging
import re
from datetime import datetime
from threading import Event, Thread
from threading import Event, Lock, Thread
import pytz
import telnetlib3
@@ -40,6 +40,7 @@ class DXCluster(SpotProvider):
self._LINE_PATTERN_ALLOW_RBN if self._allow_rbn_spots else self._LINE_PATTERN_EXCLUDE_RBN
)
self._telnet = None
self._telnet_lock = Lock()
self._thread = None
self._stop_event = Event()
@@ -49,8 +50,9 @@ class DXCluster(SpotProvider):
def stop(self):
self._stop_event.set()
if self._telnet:
self._telnet.close()
with self._telnet_lock:
if self._telnet:
self._telnet.close()
if self._thread:
self._thread.join(timeout=15)
if self._thread.is_alive():
@@ -63,7 +65,14 @@ class DXCluster(SpotProvider):
try:
self.status = "Connecting"
logger.info(f"DX Cluster {self._hostname} connecting...")
self._telnet = telnetlib3.Telnet(self._hostname, self._port)
new_telnet = telnetlib3.Telnet(self._hostname, self._port)
with self._telnet_lock:
self._telnet = new_telnet
if self._stop_event.is_set():
# stop() was called while we were connecting, close the connection rather than trying to
# read when we know it won't work
new_telnet.close()
break
self._telnet.read_until(self._login_prompt.encode("latin-1"))
self._telnet.write(f"{self._login_callsign}\n".encode("latin-1"))
connected = True