Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b95c8c7

Browse files
authored
Adds a ready event to BackgroundConsumer to wait on start. (#7499)
* Adds a ready event to BackgroundConsumer to allow waiting for the background thread to start
1 parent 58b7c3b commit b95c8c7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

api_core/google/api_core/bidi.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)