Skip to content

Commit

Permalink
lib.network: fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Oct 19, 2023
1 parent 3d93ee9 commit 4bf7f25
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions lib/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,14 @@ def connect(self):
self._is_connected = False
return False

# prevent starting connect thread twice
with self.__connect_threadlock:
self.logger.debug(f'Starting connect to {self._host}:{self._port}')
if not self.__connect_thread or not self.__connect_thread.is_alive():
self.logger.dbglow(f'connect() creating connect thread "TCP_Connect {self._id}')
self.__connect_thread = threading.Thread(target=self._connect_thread_worker, name=f'TCP_Connect {self._id}')
self.__connect_thread.daemon = True
self.logger.dgblow(f'connect() to {self._host}:{self._port}: self.__running={self.__running}, self.__connect_thread.is_alive()={self.__connect_thread.is_alive()}')
self.logger.dbglow(f'connect() to {self._host}:{self._port}: self.__running={self.__running}, self.__connect_thread.is_alive()={self.__connect_thread.is_alive()}')
if not self.__running or not self.__connect_thread.is_alive():
self.logger.dbglow(f'connect() to {self._host}:{self._port}: calling __connect_thread.start()')
try:
Expand Down Expand Up @@ -753,9 +754,6 @@ def _connect_thread_worker(self):
"""
Thread worker to handle connection.
"""
if not self.__connect_threadlock.acquire(blocking=False):
self.logger.info(f'{self._id} connection attempt already in progress, ignoring new request')
return
if self._is_connected:
self.logger.info(f'{self._id} already connected, ignoring new request')
return
Expand All @@ -769,7 +767,6 @@ def _connect_thread_worker(self):
self._connect()
if self._is_connected:
try:
self.__connect_threadlock.release()
if self._connected_callback:
self._connected_callback(self)
name = f'TCP_Client {self._id}'
Expand All @@ -795,11 +792,6 @@ def _connect_thread_worker(self):
self._connect_counter = 0
else:
break
try:
self.__connect_threadlock.release()
except Exception:
# self.logger.debug(f'{self._id} exception while trying self.__connect_threadlock.release()')
pass

def _connect(self):
"""
Expand Down

0 comments on commit 4bf7f25

Please sign in to comment.