Skip to content

fix(storage): consume server responses before finishing stream in AsyncWriter Close/Finalize - #16270

Merged
kalragauri merged 8 commits into
googleapis:mainfrom
kalragauri:feat/close-hangs
Jul 28, 2026
Merged

fix(storage): consume server responses before finishing stream in AsyncWriter Close/Finalize#16270
kalragauri merged 8 commits into
googleapis:mainfrom
kalragauri:feat/close-hangs

Conversation

@kalragauri

@kalragauri kalragauri commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

This PR fixes an issue where calling AsyncWriter::Close() or Finalize() hangs indefinitely when the GCS service returns intermediate response messages before closing the stream.

Under the hood, Close() sends a kFlushAndClose request, which prompts GCS to respond. Previously, AsyncWriterConnectionImpl called Finish() on the underlying gRPC stream immediately after writing without consuming any remaining server responses. This unread data on the stream caused Finish() to block waiting for response consumption, leading to a deadlock.

The PR also updates unit tests that mock the upload stream to expect the additional Read() call (returning EOF) and adjust the sequencer expectations.

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Jul 21, 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 refactors OnClose and OnFinalUpload in AsyncWriterConnectionImpl to use a recursive ConsumeLoop that reads and processes all intermediate server responses before finalizing the upload, preventing hangs when multiple responses are sent before EOF. Corresponding unit tests are updated and a new test is added to verify this behavior. The review feedback correctly identifies compilation errors in both refactored methods where the undefined variable self is referenced inside lambdas capturing [this], and provides code suggestions to resolve these issues by calling Finish() directly.

Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc Outdated
Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc Outdated
@kalragauri
kalragauri force-pushed the feat/close-hangs branch 4 times, most recently from 6d2b7b3 to cc63d2d Compare July 21, 2026 10:00
@kalragauri
kalragauri marked this pull request as ready for review July 21, 2026 10:22
@kalragauri
kalragauri requested review from a team as code owners July 21, 2026 10:22
@kalragauri
kalragauri force-pushed the feat/close-hangs branch 2 times, most recently from a90a56f to 28689f7 Compare July 22, 2026 12:51
@kalragauri
kalragauri requested a review from rajeevpodar July 23, 2026 04:08
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.87218% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.30%. Comparing base (f0c5fb2) to head (a6ddd0e).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...d/storage/internal/async/writer_connection_impl.cc 92.45% 4 Missing ⚠️
...rage/internal/async/writer_connection_impl_test.cc 99.16% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16270      +/-   ##
==========================================
+ Coverage   92.29%   92.30%   +0.01%     
==========================================
  Files        2221     2223       +2     
  Lines      207384   207876     +492     
==========================================
+ Hits       191402   191883     +481     
- Misses      15982    15993      +11     

☔ View full report in Codecov by Harness.
📢 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.

Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc Outdated
Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc
@kalragauri

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 updates AsyncWriterConnectionImpl to correctly handle and consume multiple response messages from the server before receiving an EOF during close and query operations. It also improves thread safety by ensuring proper mutex locking when accessing shared state. The review comments correctly identify opportunities to avoid unnecessary copies of non-fundamental protobuf messages by moving them out of futures using std::move(f).get().

Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc Outdated
Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc Outdated
Comment thread google/cloud/storage/internal/async/writer_connection_impl.cc Outdated
@kalragauri
kalragauri enabled auto-merge (squash) July 28, 2026 04:18
@kalragauri
kalragauri merged commit b9e74bb into googleapis:main Jul 28, 2026
62 of 63 checks passed
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