mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-20 14:27:42 +00:00
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:
+13
-4
@@ -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
|
||||
@@ -29,6 +29,7 @@ class RBN(SpotProvider):
|
||||
super().__init__(name, provider_config)
|
||||
self._port = provider_config["port"]
|
||||
self._telnet = None
|
||||
self._telnet_lock = Lock()
|
||||
self._thread = None
|
||||
self._stop_event = Event()
|
||||
|
||||
@@ -38,8 +39,9 @@ class RBN(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():
|
||||
@@ -52,7 +54,14 @@ class RBN(SpotProvider):
|
||||
try:
|
||||
self.status = "Connecting"
|
||||
logger.info(f"RBN port {self._port!s} connecting...")
|
||||
self._telnet = telnetlib3.Telnet("telnet.reversebeacon.net", self._port)
|
||||
new_telnet = telnetlib3.Telnet("telnet.reversebeacon.net", 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("Please enter your call: ".encode("latin-1"))
|
||||
self._telnet.write(f"{SERVER_OWNER_CALLSIGN}\n".encode("latin-1"))
|
||||
connected = True
|
||||
|
||||
Reference in New Issue
Block a user