Skip to content

Commit 61d7d5a

Browse files
Improved shutdown dispatch operations
1 parent 3793358 commit 61d7d5a

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/sttp/transport/datasubscriber.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ def dispose(self):
227227
"""
228228

229229
self._disposing = True
230-
self._connector.cancel()
230+
self._connector.dispose()
231231
self._disconnect(True, False)
232232

233-
# Allow a moment for connection terminated event to complete
234-
Event().wait(0.01) # 10ms
233+
# Wait for connection terminated event to complete
234+
self._threadpool.shutdown(wait=True)
235235

236236
@property
237237
def connected(self) -> bool:
@@ -574,15 +574,16 @@ def _run_disconnectthread(self, autoreconnecting: bool): # sourcery skip: extra
574574
# by the peer. Additionally, this allows the user to automatically reconnect in their
575575
# callback function without having to spawn their own separate thread.
576576
def _dispatch_connectionterminated(self):
577-
self._connection_terminationthread_mutex.acquire()
578-
579-
if self._connection_terminationthread is not None:
580-
return
577+
try:
578+
self._connection_terminationthread_mutex.acquire()
581579

582-
self._connection_terminationthread = Thread(target=self._handle_connectionterminated, name="ConnectionTerminationThread")
583-
self._connection_terminationthread.start()
580+
if self._connection_terminationthread is not None:
581+
return
584582

585-
self._connection_terminationthread_mutex.release()
583+
self._connection_terminationthread = Thread(target=self._handle_connectionterminated, name="ConnectionTerminationThread")
584+
self._connection_terminationthread.start()
585+
finally:
586+
self._connection_terminationthread_mutex.release()
586587

587588
def _handle_connectionterminated(self):
588589
self._disconnect(False, True)

src/sttp/transport/subscriberconnector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def __init__(self,
110110

111111
self._threadpool = ThreadPoolExecutor(thread_name_prefix="SC-PoolThread")
112112

113+
def dispose(self):
114+
"""
115+
Cleanly shuts down a `DataConnector` that is no longer being used, e.g., during a normal application exit.
116+
"""
117+
118+
self.cancel()
119+
self._threadpool.shutdown(wait=False)
120+
113121
def _autoreconnect(self, ds: DataSubscriber):
114122
if self._cancel or ds.disposing:
115123
return

0 commit comments

Comments
 (0)