fix(storage): gRPC misuse causing crashes due to concurrent writes from Flush() and Write()
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.
Summary of Changes
Hello @v-pratap, I'm Gemini Code Assist[^1]! 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()andWrite()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 fromFlush()orWrite()) is active at any given time, thereby preventing concurrent gRPC writes. -
Logic Reordering in
OnQuery: Reordered the execution flow within theOnQuerycallback to ensure thatSetFlushedis called after theOnQueryprocessing, preventing potential issues with object lifetime management. -
New Test Case: Introduced a new test case,
NoConcurrentWritesWhenFlushAndWriteRace, to specifically validate the fix and ensure that concurrentFlush()andWrite()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 :thumbsup: and :thumbsdown: 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.
[^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.
Codecov Report
:x: Patch coverage is 89.47368% with 8 lines in your changes missing coverage. Please review.
:white_check_mark: 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 :warning: |
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
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.