fix(storage): Resolve potential race condition in AsyncWriterConnectionImpl by kalragauri · Pull Request #16099 · googleapis/google-cloud-cpp · GitHub
Skip to content

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

Merged
kalragauri merged 2 commits into
googleapis:mainfrom
kalragauri:asan
May 20, 2026
Merged

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

Conversation

@kalragauri

@kalragauri kalragauri commented May 15, 2026

Copy link
Copy Markdown
Contributor

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

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

Copy link
Copy Markdown

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

codecov Bot commented May 15, 2026

Copy link
Copy Markdown

Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc
Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc
Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc
Comment thread google/cloud/storage/internal/async/writer_connection_impl.h
@kalragauri kalragauri requested a review from v-pratap May 19, 2026 07:37
@kalragauri kalragauri merged commit 49b6a27 into googleapis:main May 20, 2026
53 of 59 checks passed
@kalragauri kalragauri deleted the asan branch May 25, 2026 12:01
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.

2 participants