Skip to content

Conversation

@v-pratap
Copy link
Contributor

@v-pratap v-pratap commented Nov 27, 2025

A gRPC misuse issue can lead to crashes when using the async writer. The issue occurs due to a race condition between user code and the SDK's internal handling of Flush() operations.

Here's a breakdown of the problem:

When a Flush() is issued, the buffer is cleaned up asynchronously via OnQuery() after the flush future is marked complete. OnQuery() can then call WriteLoop(), which issues a write. However, user code can also call Write() after the Flush() future is complete, which can also call WriteLoop(). This can result in two concurrent writes to the same gRPC stream, which is against the gRPC API contract and can cause a crash.

@v-pratap v-pratap requested review from a team as code owners November 27, 2025 06:37
@product-auto-label product-auto-label bot added the api: storage Issues related to the Cloud Storage API. label Nov 27, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @v-pratap, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical race condition within the gRPC async writer that could lead to application crashes. The issue arose from Flush() and Write() operations potentially initiating concurrent writes to the same gRPC stream, violating the gRPC API contract. The solution involves introducing and carefully managing a writing_ flag to serialize write operations, ensuring only one write is active at a time. This prevents the problematic concurrent access and stabilizes the async writer. A new test case has been added to thoroughly validate this fix.

Highlights

  • Race Condition Fix: Addressed a critical gRPC misuse issue that caused crashes due to a race condition between Flush() and Write() operations, leading to concurrent writes on the same gRPC stream.
  • Concurrent Write Prevention: Implemented a writing_ flag to ensure that only one write operation (either from Flush() or Write()) is active at any given time, thereby preventing concurrent gRPC writes.
  • Logic Reordering in OnQuery: Reordered the execution flow within the OnQuery callback to ensure that SetFlushed is called after the OnQuery processing, preventing potential issues with object lifetime management.
  • New Test Case: Introduced a new test case, NoConcurrentWritesWhenFlushAndWriteRace, to specifically validate the fix and ensure that concurrent Flush() and Write() calls no longer result in concurrent gRPC writes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a critical race condition that could cause crashes due to concurrent writes on a gRPC stream. The solution, which involves introducing a writing_ flag for serialization and adjusting the order of asynchronous operations, is sound and well-implemented. The new test case, NoConcurrentWritesWhenFlushAndWriteRace, is particularly valuable as it specifically targets the race condition and confirms the fix. The changes significantly enhance the thread safety and reliability of the async writer. I have one minor style suggestion.

{
std::unique_lock<std::mutex> lk(mu_);
was_finalizing = finalizing_;
was_finalizing = finalizing_;

Choose a reason for hiding this comment

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

medium

This line contains trailing whitespace. The repository's style guide indicates it follows the Google C++ Style Guide, which advises against trailing whitespace for code cleanliness.1

      was_finalizing = finalizing_;

Style Guide References

Footnotes

@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

❌ Patch coverage is 89.47368% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.93%. Comparing base (c03c99b) to head (6b5569a).

Files with missing lines Patch % Lines
...e/internal/async/writer_connection_resumed_test.cc 87.30% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15802      +/-   ##
==========================================
- Coverage   92.94%   92.93%   -0.01%     
==========================================
  Files        2457     2457              
  Lines      227128   227190      +62     
==========================================
+ Hits       211099   211148      +49     
- Misses      16029    16042      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant