Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors AsyncWriterConnectionImpl to use an internal State class managed by a std::shared_ptr. This design change successfully prevents potential use-after-free bugs when a writer is destroyed while asynchronous operations (such as writes, flushes, or closes) are still in flight. Corresponding unit tests have been added to verify this behavior. Regarding the code quality, there are several violations of the repository style guide concerning the use of auto. Specifically, auto is used where it obscures domain objects, protobuf messages, or primitive types. It is recommended to replace these with explicit type declarations to improve readability and maintainability.
Move AsyncWriterConnectionImpl's async state into a shared_ptr<State> inheriting from std::enable_shared_from_this, and capture shared_from_this() in all streaming RPC continuations instead of raw this. Also track in-flight operations so ~AsyncWriterConnectionImpl() cancels the stream and defers impl_->Finish() until all in-flight operations complete, avoiding Finish()-while-op-pending contract violations and keeping State and the underlying StreamingRpc alive until Finish() completes.
f55ee38 to
43790ea
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16729 +/- ##
==========================================
+ Coverage 92.83% 92.84% +0.01%
==========================================
Files 2279 2278 -1
Lines 229442 230551 +1109
==========================================
+ Hits 213009 214062 +1053
- Misses 16433 16489 +56 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Fixes a use-after-free in
AsyncWriterConnectionImplwhen anAsyncWriter(such as a buffered or appendable/resumed writer) is destroyed while asynchronous streaming RPC operations are still in flight.Root Cause
With buffered (
MakeWriterConnectionBuffered) and appendable/resumed (MakeWriterConnectionResumed) uploads,Write()returns a ready future immediately when the resend buffer is below the high-water mark while the underlyingAsyncWriterConnectionImpl::Write()is still in flight. Previously:AsyncWriterConnectionImplcaptured rawthisin all async streaming RPC continuations (Write,Finalize,Flush,Close,OnFlush,OnClose,OnFinalUpload).~AsyncWriterConnectionImpl()calledFinish()immediately even when an operation was still in flight, violatingAsyncStreamingReadWriteRpc::Finish()'s precondition and freeingAsyncWriterConnectionImplbefore pending callbacks executed.When the in-flight RPC later completed, the continuation dereferenced freed
AsyncWriterConnectionImplmembers (heap-use-after-free under ASan).Changes
AsyncWriterConnectionImpl's state and async callbacks intostd::shared_ptr<AsyncWriterConnectionImpl::State>inheriting fromstd::enable_shared_from_this<State>, preservingstd::unique_ptr/ stack ownership ofAsyncWriterConnectionImpl.thiscaptures in async continuations (Write,Finalize,Flush,Close,ConsumeLoop,Finish) withshared_from_this().in_flight_) andshutdown_undermu_:~AsyncWriterConnectionImpl()cancels the stream and defersimpl_->Finish()until all in-flight operations complete, extendingState(andimpl_) lifetime untilimpl_->Finish()completes.writer_connection_impl_test.cc:Write(succeeds/fails),Flush(write phase and read phase),Finalize, andClose.writer_connection_buffered_test.cc:WriterConnectionBufferedDestroyTest.PendingWriteOutlivesConnection(WriteSucceeds/WriteFails).writer_connection_resumed_test.cc:WriterConnectionResumedDestroyTest.PendingWriteOutlivesConnection(WriteSucceeds/WriteFails).