Repository navigation
Core: Race condition in bidi.BackgroundConsumer? #15094
Description
Environment details
Any OS, any Python version, google-api-core==1.9.0.
Steps to reproduce
No actual steps, spotted this in bidi.py while working on a different issue. This is how the "Am I paused?" snippet in BackgroundConsumer._thread_main() looks like:
with self._wake:
if self._paused:
_LOGGER.debug("paused, waiting for waking.")
self._wake.wait()
_LOGGER.debug("woken.")
_LOGGER.debug("waiting for recv.")
response = self._bidi_rpc.recv()The _paused state can be set / cleared by the pause() and resume() methods, respectively.
If paused, the code snippet from above blocks at the _wake.wait() call, and is unblocked some time after _wake.notifyAll() is invoked in the resume() method. When resume() method notifies the waiting threads and releases the internal lock held by the self._wake condition, _wake.wait() tries to re-obtain that lock.
Now, suppose that some other thread invokes the pause() method in the meantime, and the latter obtains the self._wake's lock before _wake.wait() can grab it. The _paused flag will again be set to True, and when _wake.wait() finally acquires the lock and resumes, the code will invoke _bidi_rpc.recv() in the paused state.
For this reason the self._paused condition must be checked in a loop, and not in a single if statement, as the docs on threading.Condition state:
The while loop checking for the application’s condition is necessary because
wait()can return after an arbitrary long time, and the condition which prompted thenotify()call may no longer hold true. This is inherent to multi-threaded programming.
How much of a problem this is, i.e. invoking recv() in a paused state?
Edit:
The same can happen when exiting the with self._wake block. The lock gets released, pause() can acquire it and set _self._paused to True, but _bidi_rpc.recv() will nevertheless be called, because it is placed outside the with block.
Code example
I created a demo script that demonstrates why if <condition> is not sufficient, and subject to race conditions (requires Python 3.6+). The "worker" thread waits until a shared global number is set to 10, while five "changer threads" randomly change that number, and notify other threads if they change it to 10.
Sometimes the "worker" is invoked too late, and another "changer" thread changes 10 to something else, causing the "worker" to continue running when the target condition is no longer true. Replacing if not <condition> with while not <condition> gets rid of the bug.
...
INFO [2019-04-29 23:09:20,327] Thread-Changer-0: Changing number from 9 to 10
INFO [2019-04-29 23:09:20,327] Thread-Changer-0: New number == 10, notifying others
INFO [2019-04-29 23:09:20,327] Thread-Worker : I see the number 10
INFO [2019-04-29 23:09:20,327] Thread-Changer-1: Changing number from 10 to 4
INFO [2019-04-29 23:09:20,327] Thread-Changer-4: Changing number from 4 to 10
INFO [2019-04-29 23:09:20,327] Thread-Changer-4: New number == 10, notifying others
INFO [2019-04-29 23:09:20,328] Thread-Changer-2: Changing number from 10 to 3
INFO [2019-04-29 23:09:21,328] Thread-Changer-3: Changing number from 3 to 9
INFO [2019-04-29 23:09:21,328] Thread-Changer-1: Changing number from 9 to 6
INFO [2019-04-29 23:09:21,328] Thread-Changer-4: Changing number from 6 to 4
INFO [2019-04-29 23:09:21,328] Thread-Worker : the number is not 10, will wait for condition
INFO [2019-04-29 23:09:21,328] Thread-Changer-0: Changing number from 4 to 6
INFO [2019-04-29 23:09:21,329] Thread-Changer-2: Changing number from 6 to 9
INFO [2019-04-29 23:09:22,329] Thread-Changer-3: Changing number from 9 to 10
INFO [2019-04-29 23:09:22,329] Thread-Changer-3: New number == 10, notifying others
INFO [2019-04-29 23:09:22,329] Thread-Changer-1: Changing number from 10 to 5
INFO [2019-04-29 23:09:22,329] Thread-Worker : I see the number 5
...
The expected behavior is that the worker thread always prints out "I see the number 10" (the desired condition), and never "I see <non 10>". In other words, it should only proceed when the condition number == 10 is currently fulfilled.
In the bidi.py case, this would translate to BackgroundConsumer._thread_main() only resuming when self._paused == False, and never resuming when self._paused == True.
Activity
tseaver commentedon Jul 23, 2019
@plamut I'm in agreement with your analysis here: the stdlib's docs hightlght the problem clearly.
tseaver commentedon Jul 23, 2019
The "obvious" fix:
passes all unit tests in
api_core, but causes system tests to hang for firestore:and
pubsub:plamut commentedon Jul 23, 2019
Did a quick check (for
pubsubat least) with the above patch applied, and the async callbacks test hangs when cancelling the future. Might be related to #8616.Update:
Or maybe not. It seems that the problem here is waiting for recv() while holding the
self._wakelock. If the future gets cancelled then, the consumer tries to resume the thread, but resume() method first needs to acquire the very sameself._wakelock and gets blocked.Fix race in 'BackgroundConsumer._thread_main'. (#8883)
tseaver commentedon Aug 1, 2019
@plamut believes that #8883 did not completely resolve the race, so I updated it to avoid closing this issue.
tseaver commentedon Oct 10, 2019
Both
BidiRpc.recvandResumableBidiRpc.recvare implemented by callingnext()on the underling gRPCCallobject, which provides no timeout mechanism.PR #9337 deals with the race / hang by timing out the call to join the daemonized worker thread, but it doesn't really fix the issue here.
I've filed grpc/grpc#20562 to ask that the
grpc._channel._Rendezvousclass expose an alternative method which takes a timeout: we could then close this issue by calling that method rather than usingnext(self.call).vchudnov-g commentedon Jan 31, 2023
UPDATE: PR #9337 was merged, but it's not immediately clear whether grpc/grpc#20562 was addressed. Pinging this issue so we consider it in future planning.
vchudnov-g commentedon May 19, 2023
Pinged grpc/grpc#20562 to see whether that was implemented, which would unblock us from implementing the rest of the solution here.
vchudnov-g commentedon Jul 23, 2024
It looks like grpc/grpc#20562 won't happen this quarter at least. We're waiting on that.
vchudnov-g commentedon Apr 21, 2025
I don't see any progress on grpc/grpc#20562; i just pinged them for an update.
parthea commentedon Jan 12, 2026
Transferring to google-cloud-python as the code for python-api-core will be migrated there in 1-3 weeks.
daniel-sanche commentedon May 28, 2026
This issue is being bulk closed due to inactivity. Please re-open if you think this still requires attention, and we'll take another look. Thanks!