Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces robust cancellation handling and state tracking in OpenObject and OpenStream to ensure safe, concurrent teardown of bidirectional streaming RPCs, supported by comprehensive new unit tests. The review feedback recommends aligning with the repository style guide by avoiding auto where it obscures domain or return types—specifically for std::shared_ptr<OpenObject> and future<StatusOr<OpenStreamResult>>—and suggests removing a redundant return statement from a void function for consistency.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16613 +/- ##
==========================================
+ Coverage 92.76% 92.79% +0.02%
==========================================
Files 2273 2273
Lines 227827 228066 +239
==========================================
+ Hits 211355 211623 +268
+ Misses 16472 16443 -29 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
184911f to
bb6e5de
Compare
bb6e5de to
ac48a20
Compare
Note
Stacked PR (1 of 2): Standalone cancellation-safety prerequisite for #16612 (
feat(storage): add speculative open hedging to AsyncClient::Open). Can be reviewed and merged independently (6 files, +375 / -55).Summary
Makes
storage_internal::OpenStreamandstorage_internal::OpenObjectsafe to cancel mid-handshake using a single-mutex operation counter inOpenStreamand wiresfuture::cancel()onOpenObject::Call()so cancelledBidiReadObjectopens abort cleanly and resolve only afterFinish()completes.Motivation
OpenStreamandOpenObjectpreviously had two cancellation hazards when anOpen()handshake (Start()-> initialWrite()-> firstRead()) was cancelled before completing:OpenStreammid-Start()/ concurrent cancellation & lifecycle races:OpenStreamtrackedpending_read_andpending_write_, but notStart(). CallingOpenStream::Cancel()whilerpc_->Start()was still in flight immediately invokedFinish(), violating gRPC's requirement not to callFinish()whileStart()is pending.OpenStream::Cancel()was not idempotent,Start()/Write()/Read()checkedcancel_before setting their pending flags (leaving a check-then-set window where a concurrentCancel()could callFinish()concurrently with a newly launched gRPC operation), andMaybeFinish()discarded itsFinish()future soOpenObject::DoFinish()could not awaitFinish()completion.Finish()ran before an in-progressCancel()completed onAsyncStreamingReadWriteRpcAuth,Auth::Finish()markedcancelled = truefirst and causedAuth::Cancel()to skipTryCancel()on an open stream.OpenObjectpromise cancellation & commit race:OpenObject::Call()created apromise<StatusOr<OpenStreamResult>>without a cancellation callback, so calling.cancel()on the returnedfuturedid not cancel the underlyingOpenStream.future::cancel()raced withOnRead()committing the opened stream to the caller (or withOnFinish()draining a failing handshake), an uncoordinated cancel could tear down a stream already handed off toObjectDescriptorImplor misreportOK/kUnavailable/kInternalinstead ofkCancelled(causing outerAsyncRetryLoopcallers to retry a cancelled open).Changes
google/cloud/storage/internal/async/open_stream.{h,cc}:std::atomic<bool>flags (cancel_,pending_read_,pending_write_,finish_issued_) with a singlestd::mutex mu_andint pending_ops_counter (BeginOp()/EndOp()) coveringStart(),Write(),Read(), andCancel()itself, with zerorpc_->*calls made while holdingmu_.rpc_->Finish()whenFinish()is called on an uncancelled stream (finish_promise_{null_promise_t{}}), while deferringrpc_->Finish()on cancelled streams untilrpc_->Cancel()and all in-flight operations drain (pending_ops_ == 0), handing that pendingfinish_future to the firstFinish()caller.std::shared_ptr<OpenStream>(self = std::move(s)) out of.then()continuations and callsEndOp()beforef.get()so streams never leak via retained upstreamfuture_shared_statecontinuations, while masking post-cancel completions (Start/Write->false,Read->std::nullopt).google/cloud/storage/internal/async/open_object.{h,cc}:CreateRpc(),OnStart(),OnWrite(), andDoFinish()unchanged frommain, initializespromise_{null_promise_t{}}to avoid a throwaway shared-state allocation in the constructor, and wirespromise_cancellation toOpenObject::OnCancel().kOpening -> kCommitted | kCancelled) withstd::move(rpc_)on commit so late cancellations never tear down an already-committed stream, calls idempotentrpc_->Cancel()beforeDoFinish()ifOnRead()loses the commit race toOnCancel(), and checksstate_ == State::kCancelledbefore!status.ok()inOnFinish()to unconditionally returnStatusCode::kCancelled.open_stream_test.cc: VerifiesInSequenceCancel-before-Finishordering inCancelBlocksAllRequest,DuplicateFinish(!Finish().valid()),CancelDuringStartDefersFinishAndHandsOffFutureOnce, andCancelDuringReadDropsResponseAndReleasesRetainedPromise(includingweak_ptrrelease with a retained upstreampromise).open_object_test.cc: Adds parameterizedOpenObjectCancelPhaseTest.CancelsCleanlyAtHandshakePhase(kStart,kWrite,kRead,kFinish— verifyingCancel/Finishordering, post-Finishreadiness, andkCancelledprecedence overkUnavailable) and multi-threadedOpenImpl.ConcurrentCancelAndReadCompletion.