google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

storage: AsyncMultiRangeDownloader loses object metadata when initial open is redirected with a read_handle

Open yuxin00j opened this issue 2 weeks ago • 0 comments

Problem

When AsyncMultiRangeDownloader.create_mrd(..., read_handle=None) (or open() without a caller-supplied read_handle) receives an Aborted BidiReadObjectRedirectedError carrying both routing_token and read_handle, AsyncMultiRangeDownloader._on_open_error() unconditionally adopts redirect.read_handle and retries open() with BidiReadObjectSpec.read_handle set.

Per google/storage/v2/storage.proto, BidiReadObjectResponse.metadata is not populated when a stream is opened with a read_handle. As a result, create_mrd() returns with persisted_size=None, is_finalized=False, full_obj_server_crc32c=None, and generation=None (when generation was not passed), even though the caller did not supply a read_handle. Callers that rely on persisted_size—for example when reading an unfinalized appendable object whose live size is not yet reflected elsewhere—receive None.

Expected behavior

When opening without a caller-supplied read_handle, a redirect that includes a routing_token should retry with the routing_token only so the initial response includes BidiReadObjectResponse.metadata (populating persisted_size, is_finalized, full_obj_server_crc32c, and generation) along with a fresh read_handle for subsequent streams.

Proposed fix

In AsyncMultiRangeDownloader._on_open_error(), only adopt redirect.read_handle if self.read_handle is already set (caller supplied a handle or a handle was already obtained) or the redirect has no routing_token. Otherwise retry with routing_token alone.

yuxin00j avatar Nov 19 '26 08:11 yuxin00j