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):
505
505
# when the RPC has terminated.
506
506
self .resume ()
507
507
508
- def _thread_main (self ):
508
+ def _thread_main (self , ready ):
509
509
try :
510
+ ready .set ()
510
511
self ._bidi_rpc .add_done_callback (self ._on_call_done )
511
512
self ._bidi_rpc .open ()
512
513
@@ -555,11 +556,19 @@ def _thread_main(self):
555
556
def start (self ):
556
557
"""Start the background thread and begin consuming the thread."""
557
558
with self ._operational_lock :
559
+ ready = threading .Event ()
558
560
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 ,)
560
564
)
561
565
thread .daemon = True
562
566
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 ()
563
572
self ._thread = thread
564
573
_LOGGER .debug ("Started helper thread %s" , thread .name )
565
574
You can’t perform that action at this time.
0 commit comments