Skip to content

fix(storage): Resolve potential race condition in AsyncWriterConnectionImpl#16099

Open
kalragauri wants to merge 2 commits into
googleapis:mainfrom
kalragauri:asan
Open

fix(storage): Resolve potential race condition in AsyncWriterConnectionImpl#16099
kalragauri wants to merge 2 commits into
googleapis:mainfrom
kalragauri:asan

Conversation

@kalragauri
Copy link
Copy Markdown
Contributor

@kalragauri kalragauri commented May 15, 2026

This change addresses a potential race condition in AsyncWriterConnectionImpl between the destructor and asynchronous callback operations.

The impl_ member variable could be moved in the destructor while simultaneously being accessed by a callback function (e.g., within OnPartialUpload which can call impl_->Finish()). This concurrent access could lead to a use-after-free error under certain timing conditions. To resolve this, this commit introduces a std::mutex to protect accesses to the impl_ member.

Why other methods (Write, Flush, etc.) are not protected:

The library's public API contract forbids users from calling methods like Write(), Flush(), Query(), or Finalize() while destruction is in progress or while another operation is already pending. As long as the user follows this contract, these methods will never run concurrently with the destructor.

The only reason we need protection in Finish() and OnFinalUpload() is because they are called by the library's own internal asynchronous callbacks (running on the Completion Queue thread), which can execute concurrently with the destructor (running on the user's thread) if an operation completes or fails just as the object is being destroyed.

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label May 15, 2026
Copy link
Copy Markdown

@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 introduces a mutex to protect the impl_ member variable during destruction and within the Finish method of AsyncWriterConnectionImpl to prevent data races. While these changes improve thread safety, the reviewer noted that the fix is incomplete as other methods accessing impl_ and shared state like latest_write_handle_ remain unprotected, potentially leading to race conditions or null pointer dereferences.

Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc
@kalragauri kalragauri marked this pull request as ready for review May 15, 2026 05:19
@kalragauri kalragauri requested review from a team as code owners May 15, 2026 05:19
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.68%. Comparing base (e5d585c) to head (0e5e0c3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16099      +/-   ##
==========================================
- Coverage   92.69%   92.68%   -0.01%     
==========================================
  Files        2353     2353              
  Lines      218328   218339      +11     
==========================================
- Hits       202386   202375      -11     
- Misses      15942    15964      +22     

☔ 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