Skip to content

fix(storage): close the write stream when opening it fails - #19324

Open
Yonghui-Lee wants to merge 1 commit into
googleapis:mainfrom
Yonghui-Lee:fix/storage-write-stream-close-on-open-error
Open

Yonghui-Lee wants to merge 1 commit into
googleapis:mainfrom
Yonghui-Lee:fix/storage-write-stream-close-on-open-error

Conversation

@Yonghui-Lee

Copy link
Copy Markdown

Summary

_AsyncWriteObjectStream.open doesn't close its AsyncBidiRpc when open() or the first recv() raises. The read stream already does this through _close_socket_like_rpc. The appendable writer's retry only closes the old stream when is_stream_open is true, and it's false here, so nobody closes it.

On Rapid buckets every writer open first gets Aborted (the zonal redirect) and then retries with the routing token. So each Rapid write leaves one pending gRPC _consume_request_iterator task behind. A failed create-only open (generation=0 on an existing object) leaves two. At interpreter exit they show up as Task was destroyed but it is pending! warnings.

Changes

  • Wrap socket_like_rpc.open() and the first recv() in _AsyncWriteObjectStream.open. On any exception, including asyncio.CancelledError, close the RPC and re-raise the original error.
  • Add _AsyncWriteObjectStream._close_socket_like_rpc, which closes the RPC and logs close errors at debug level so they don't hide the original error.
  • is_stream_open stays false after a failed open, as before.
  • Add unit tests for failures in open() and recv() (Aborted, FailedPrecondition, CancelledError) and for a close that itself fails.

Testing

  • tests/unit/asyncio/test_async_write_object_stream.py and test_async_appendable_object_writer.py pass. The 7 new test cases fail without the fix.
  • On a real Rapid bucket with 3.17.1, one write plus three failed create-only opens produced 7 pending-task warnings. With this change it produced 0, with the same retry behavior (4 Aborted, 3 FailedPrecondition).

_AsyncWriteObjectStream.open did not close its AsyncBidiRpc when open() or
the first recv() raised. On Rapid buckets every open first gets Aborted
(zonal redirect) and the writer retries with a new stream, so each write
left one pending gRPC request task behind. A failed create-only open
(generation=0) left two. Close the RPC on failure, like the read stream
already does, and re-raise the original error.

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

Copy link
Copy Markdown
Contributor

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 ensures that the gRPC bidi-stream is properly closed if an error or cancellation occurs during the open() call in _AsyncWriteObjectStream, preventing request task leaks. It introduces a helper method _close_socket_like_rpc to handle stream closure safely and adds comprehensive unit tests to verify this behavior under various failure scenarios. No review comments were provided, and the implementation looks correct.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant