File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed
Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -505,8 +505,9 @@ def _on_call_done(self, future):
505505 # when the RPC has terminated.
506506 self .resume ()
507507
508- def _thread_main (self ):
508+ def _thread_main (self , ready ):
509509 try :
510+ ready .set ()
510511 self ._bidi_rpc .add_done_callback (self ._on_call_done )
511512 self ._bidi_rpc .open ()
512513
@@ -555,11 +556,19 @@ def _thread_main(self):
555556 def start (self ):
556557 """Start the background thread and begin consuming the thread."""
557558 with self ._operational_lock :
559+ ready = threading .Event ()
558560 thread = threading .Thread (
559- name = _BIDIRECTIONAL_CONSUMER_NAME , target = self ._thread_main
561+ name = _BIDIRECTIONAL_CONSUMER_NAME ,
562+ target = self ._thread_main ,
563+ args = (ready ,)
560564 )
561565 thread .daemon = True
562566 thread .start ()
567+ # Other parts of the code rely on `thread.is_alive` which
568+ # isn't sufficient to know if a thread is active, just that it may
569+ # soon be active. This can cause races. Further protect
570+ # against races by using a ready event and wait on it to be set.
571+ ready .wait ()
563572 self ._thread = thread
564573 _LOGGER .debug ("Started helper thread %s" , thread .name )
565574
You can’t perform that action at this time.
0 commit comments