websocket restarting issue. The websocket app is not entering in its on_close function from time to time

Viewed 18

I am having the following issue using the standard websocket lib for my socket connection, called websocket-client ver0.59.0 I am restarting the websocket from time to time (in this case every 3 hours or so) on another thread with the following code

def reset(ws):
while True:
    time.sleep(10000)
    print(f"RESTARTING THE SOCKET NOW-->{time.time()}")
    ws.close()

normally when my socket is restarted the messages in my console are the following:

RESTARTING THE SOCKET NOW-->Tue Sep 13 18:40:30 2022
websocket closed
disconnected
Start websocket connection-->2022-09-13 18:40:31

From this messages I can understand that: 1st The thread while true is working fine 2nd the program is entering in the def _on_close(ws) function of the websocket app 3rd the connection is started again

Now the issue: After more than 24 hours (more than 8 resets) the following error appears in my console:

RESTARTING THE SOCKET NOW-->Tue Sep 13 21:28:30 2022
disconnected
Start websocket connection-->2022-09-13 21:28:31
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\stef...\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\Users\stef....\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\stef....websocket_1.py", line 122, in _run
.....
    ws.run_forever()
  File "C:\Users\stef....\venv\lib\site-packages\websocket\_app.py", line 266, in run_forever
    raise WebSocketException("socket is already opened")
websocket._exceptions.WebSocketException: socket is already opened
Traceback (most recent call last):
  File "C:\Users\stef....websocket_1.py", line 315, in <module>
    time.sleep(1)

From this messages I can understand that: 1st The thread while true is working fine 2nd the program is NOT entering in the def _on_close(ws) function of the websocket app 3rd the connection is started again, but because it is not closed the program is stuck

Does anyone know why this is happening ? Why the websocket app is not entering in its on_close function?

0 Answers
Related