fix(storage): Resolve potential race condition in AsyncWriterConnectionImpl#16099
Open
kalragauri wants to merge 2 commits into
Open
fix(storage): Resolve potential race condition in AsyncWriterConnectionImpl#16099kalragauri wants to merge 2 commits into
kalragauri wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change addresses a potential race condition in
AsyncWriterConnectionImplbetween 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., withinOnPartialUploadwhich can callimpl_->Finish()). This concurrent access could lead to a use-after-free error under certain timing conditions. To resolve this, this commit introduces astd::mutexto protect accesses to theimpl_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.