Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extremely rare race conditions in retry loop #7789

Merged

Conversation

coryan
Copy link
Contributor

@coryan coryan commented Dec 28, 2021

Fixes #7788

I could not write a unit test that would reliably introduce the race condition.


This change is Reviewable

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: 24713c131758e4cc5d7c59fd2fcd0dc0c2e7f3f3

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@codecov
Copy link

codecov bot commented Dec 28, 2021

Codecov Report

Merging #7789 (0a527c1) into main (0c09efc) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7789      +/-   ##
==========================================
- Coverage   95.16%   95.15%   -0.01%     
==========================================
  Files        1278     1278              
  Lines      115102   115114      +12     
==========================================
+ Hits       109536   109539       +3     
- Misses       5566     5575       +9     
Impacted Files Coverage Δ
google/cloud/internal/async_retry_loop.h 100.00% <100.00%> (+3.09%) ⬆️
google/cloud/internal/async_retry_loop_test.cc 99.39% <100.00%> (+0.03%) ⬆️
google/cloud/bigtable/internal/common_client.h 94.02% <0.00%> (-5.98%) ⬇️
google/cloud/pubsub/subscriber_connection_test.cc 97.18% <0.00%> (-0.71%) ⬇️
google/cloud/bigtable/async_read_stream_test.cc 97.32% <0.00%> (-0.67%) ⬇️
google/cloud/pubsub/samples/samples.cc 92.02% <0.00%> (-0.32%) ⬇️
...cloud/pubsub/internal/subscription_session_test.cc 97.75% <0.00%> (-0.25%) ⬇️
...ud/spanner/integration_tests/client_stress_test.cc 86.18% <0.00%> (+0.65%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c09efc...0a527c1. Read the comment docs.

@coryan coryan marked this pull request as ready for review December 28, 2021 18:13
@coryan coryan requested a review from a team as a code owner December 28, 2021 18:13
@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: 45d5ddee5cdd72c5357b94bf92494c8e32c9279d

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

Copy link
Member

@dbolduc dbolduc left a comment

Choose a reason for hiding this comment

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

Logically, this LGTM. The documentation was helpful for understanding why we tag each operation with the iteration count.

*
* - The initial value of `cancelled_` is false.
* - `Cancel()` is the only operation that changes `cancelled_`, and it holds
* the `mu_` mutex while doing son.
Copy link
Member

Choose a reason for hiding this comment

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

s/son/so/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

* starts, as it is their callbacks who start the next step.
* - `StartIteration()` is always sequenced-before calls to `SetPending()`.
* - `SetPending()` never sets the `pending_operation_` to the future for
* an operation that already completed.
Copy link
Member

Choose a reason for hiding this comment

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

s/that already/that has already/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

* - `StartIteration()` and `SetPending()` both lock the same mutex `mu_` as
* `Cancel()`.
* - It follows that if `Cancel()` is invoked, then the `true` value will be
* visible any future calls to `StartIteration()` or `SetPending()`.
Copy link
Member

Choose a reason for hiding this comment

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

s/visible/visible to/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

bool cancelled_ = false;
bool done_ = false;
std::uint_fast32_t iteration_ = 0;
Copy link
Member

Choose a reason for hiding this comment

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

TIL.

Do you want: s/fast32/fast64/ to match State::iteration, SetPending ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed.

* as both `StartAttempt()` and `StartBackoff()` return immediately in this
* case.
* - Note that if `cancelled_` is true, `StartIteration()` terminates the
* retry loop by calling `SetDoneWithCancel()` if `cancelled_` is true.
Copy link
Member

Choose a reason for hiding this comment

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

s/`SetDoneWithCancel()` if `cancelled_` is true./`SetDoneWithCancel()`./

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: d049a6cae17a395c52142d4f8ab3f3db61da165b

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: 0a527c1b95057f58091850d434d843c4529e77fc

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@coryan coryan merged commit 2b9e0fb into googleapis:main Dec 28, 2021
@coryan coryan deleted the fix-race-conditions-in-async-retry-loop branch December 28, 2021 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extremely unlikely race conditions in internal::AsyncRetryLoop()
3 participants