@@ -152,14 +152,22 @@ def __init__(self, hub, name=None, description=None, metadata=None,
152152
153153 self.hub = hub
154154
155+ self._registration_lock = threading.Lock()
156+ self._registered_event = threading.Event()
155157 if self._callable:
156158 self._thread = threading.Thread(target=self._serve_forever)
157159 self._thread.daemon = True
158160
159161 def _serve_forever(self):
160162 while self.is_running:
163+ # Wait until we are actually registered before trying to do
164+ # anything, to avoid busy looping
161165 # Watch for callbacks here
162- if self._is_registered:
166+ self._registered_event.wait()
167+ with self._registration_lock:
168+ if not self._is_registered:
169+ return
170+
163171 results = self.hub.pull_callbacks(self.get_private_key(), 0)
164172 for result in results:
165173 if result['samp.methodName'] == 'receiveNotification':
@@ -205,11 +213,20 @@ def register(self):
205213 self.declare_metadata()
206214
207215 self._is_registered = True
216+ # Let the client thread proceed
217+ self._registered_event.set()
208218
209219 else:
210220 raise SAMPClientError("Unable to register to the SAMP Hub. Hub "
211221 "proxy not connected.")
212222
223+ def unregister(self):
224+ # We have to hold the registration lock if the client is callable
225+ # to avoid a race condition where the client queries the hub for
226+ # pushCallbacks after it has already been unregistered from the hub
227+ with self._registration_lock:
228+ super().unregister()
229+
213230
214231class SAMPIntegratedWebClient(SAMPIntegratedClient):
215232 """
0 commit comments