Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

API Core: Fix race in 'BackgroundConsumer._thread_main'.#8883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tseaver merged 3 commits intogoogleapis:masterfromtseaver:7817-api_core-fix-bidi-race
Aug 1, 2019
Merged

API Core: Fix race in 'BackgroundConsumer._thread_main'.#8883

tseaver merged 3 commits intogoogleapis:masterfromtseaver:7817-api_core-fix-bidi-race
Aug 1, 2019

Conversation

@tseaver
Copy link
Contributor

@tseavertseaver commentedAug 1, 2019
edited
Loading

See #7817.

In addition to passing allapi_core tests locally, I have verified that thefirestore andpubsub system tests all run cleanly with this patch (an earlier version caused them to hang).

@tseavertseaver added api: pubsubIssues related to the Pub/Sub API. api: core api: firestoreIssues related to the Firestore API. labelsAug 1, 2019
@googlebotgooglebot added the cla: yesThis human has signed the Contributor License Agreement. labelAug 1, 2019
- Bogus changes to pubsub and firestore to ensure their systests pass on CI.
@tseavertseaver added the kokoro:force-runAdd this label to force Kokoro to re-run the tests. labelAug 1, 2019
@yoshi-kokoroyoshi-kokoro removed the kokoro:force-runAdd this label to force Kokoro to re-run the tests. labelAug 1, 2019
Copy link
Contributor

@plamutplamut left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks good, it solves one part of the issue.

There is still a possibility that Consumer gets paused right after we release the lock, and just before we fetch a new response from the server. However, I don't think there is a straightforward way of addressing that

Since theself._bidi_rpc.recv() might block, we must release the_wake lock, otherwise shutting down the background consumer could get blocked because of it (details). This was nicely caught by system tests with an earlier version of the fix.

All in all this PR represents an improvement overall, thus approving.

@tseavertseaver merged commit5c7246f intogoogleapis:masterAug 1, 2019
@tseavertseaver deleted the 7817-api_core-fix-bidi-race branchAugust 1, 2019 21:05
@tseaver
Copy link
ContributorAuthor

@plamut I updated the PR description / commit message to avoid closing #7817. Can you please clarify how we might reproduce / fix the remaining part?

@plamut
Copy link
Contributor

Reproducing the issue

I did not manage to reproduce it with a real application (the time window is narrow), but I came up with a test that detects ifrecv() is called inBackgroundConsumer when the latter is in paused state (should not happen) -https://gist.github.com/plamut/8f996fdc9113e8de2b2c050befb36ff6

The idea is to constantly pause/resume the consumer and check if the latter is indeed paused whilerecv() is being executed.

The test consistently fails on my machine, but also passes if one comments out the following:

pause_resume_thread.start()

Can anyone confirm my findings and proof-read the test?

Fixing the issue

Apparently, we must be holding theself._wake lock when receiving messages, otherwise another thread can pause the consumer in the meantime, and the messages will be received and delivered in a paused state.

However, simply indenting the following block into thewith self._wake: context would introduce other, more serious, problems:

_LOGGER.debug("waiting for recv.")response=self._bidi_rpc.recv()_LOGGER.debug("recved response.")self._on_response(response)

Sinceself._bidi_rpc.recv() might block, holding theself._wake lock would also block stopping the consumer, for instance, because that operation would also try to obtain the very sameself._wake lock (details).

Is there a good way to make therecv() method non-blocking? Probably not?

Estimation of the issue impact

I'm not familiar with Firestore, thus I can only say for PubSub.

The background consumer gets paused when the streaming pull manager determines that the client currently has more thanMAX_LOAD messages on its hands, and resumes the consumer when there is enough capacity.

If the consumermain loop detects the paused state too late, one extra batch of server messages will be received before pausing in the next iteration.

For the PubSub client, this would mean receiving one extra batch of messages that would sit in the holding buffer until they can be processed. The extra memory usage is thus bounded by the maximum size of a single response from the server - probably not a showstopper.

parthea pushed a commit that referenced this pull requestNov 24, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@crwilcoxcrwilcoxAwaiting requested review from crwilcox

@busunkim96busunkim96Awaiting requested review from busunkim96

@anguillanneufanguillanneufAwaiting requested review from anguillanneuf

@frankynfrankynAwaiting requested review from frankyn

1 more reviewer

@plamutplamutplamut approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

api: coreapi: firestoreIssues related to the Firestore API.api: pubsubIssues related to the Pub/Sub API.cla: yesThis human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@tseaver@plamut@googlebot@yoshi-kokoro

[8]ページ先頭

©2009-2025 Movatter.jp