@@ -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 )
0 commit comments