Skip to content

Commit 9f07bb8

Browse files
committed
another race condition based on initial delay to connect
1 parent f7be18b commit 9f07bb8

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

pyghthouse/connection/wsconnector.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
from enum import Enum
12
from threading import Thread, Lock
23
from websocket import WebSocketApp, setdefaulttimeout, ABNF
34
from msgpack import packb, unpackb
45
from ssl import CERT_NONE
56

67

8+
class ConnectionState(Enum):
9+
NONE=0
10+
CONNECTING=1
11+
CONNECTED=2
12+
FAILED=3
713
class WSConnector:
814

915
class REID:
@@ -26,7 +32,7 @@ def __init__(self, username: str, token: str, address: str, on_msg=None, ignore_
2632
self.ws = None
2733
self.lock = Lock()
2834
self.reid = self.REID()
29-
self.running = False
35+
self.__connection_state = ConnectionState.NONE
3036
self.ignore_ssl_cert = ignore_ssl_cert
3137
setdefaulttimeout(60)
3238

@@ -40,26 +46,33 @@ def start(self):
4046
on_message=None if self.on_msg is None else self._handle_msg,
4147
on_open=self._ready, on_error=self._fail)
4248
self.lock.acquire() # wait for connection to be established
49+
self.__connection_state = ConnectionState.CONNECTING
4350
kwargs = {"sslopt": {"cert_reqs": CERT_NONE}} if self.ignore_ssl_cert else None
4451
Thread(target=self.ws.run_forever, kwargs=kwargs).start()
52+
4553

4654
def _fail(self, ws, err):
55+
self.__connection_state = ConnectionState.FAILED
4756
self.lock.release()
4857
raise err
4958

5059
def stop(self):
5160
if self.ws is not None:
5261
with self.lock:
5362
print("Closing the connection.")
54-
self.running = False
63+
self.__connection_state = ConnectionState.NONE
5564
self.ws.close()
5665
self.ws = None
5766

5867
def _ready(self, ws):
5968
print(f"Connected to {self.address}.")
60-
self.running = True
69+
self.__connection_state = ConnectionState.CONNECTED
6170
self.lock.release()
6271

72+
@property
73+
def connection_state(self):
74+
return self.__connection_state
75+
6376
def _handle_msg(self, ws, msg):
6477
if isinstance(msg, bytes):
6578
msg = unpackb(msg)

pyghthouse/ph.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77

88
from pyghthouse.data.canvas import PyghthouseCanvas
9-
from pyghthouse.connection.wsconnector import WSConnector
9+
from pyghthouse.connection.wsconnector import WSConnector,ConnectionState
1010

1111

1212
class VerbosityLevel(Enum):
@@ -208,10 +208,12 @@ def connect(self):
208208
self.connector.start()
209209

210210
def start(self):
211-
if not self.connector.running:
211+
if not self.connector.connection_state==ConnectionState.CONNECTED:
212212
self.connect()
213213
self.stop()
214214
self.msg_handler.reset()
215+
while not self.connector.connection_state==ConnectionState.CONNECTED:
216+
sleep(100)
215217
self.ph_thread = self.PHThread(self)
216218
self.ph_thread.start()
217219

0 commit comments

Comments
 (0)