Skip to content

Commit aebc22e

Browse files
committed
fix(storage): re-open gRPC stream on finalize retry attempt
1 parent 91d5f1b commit aebc22e

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

packages/google-cloud-storage/google/cloud/storage/asyncio/async_appendable_object_writer.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,19 @@ async def finalize(
681681
if retry_policy is None:
682682
retry_policy = AsyncRetry(predicate=_is_write_retryable)
683683

684+
attempt_count = 0
685+
684686
async def _do_finalize():
687+
nonlocal attempt_count
688+
attempt_count += 1
689+
690+
if attempt_count > 1:
691+
logger.info(
692+
f"Re-opening the stream for finalize retry attempt: {attempt_count}"
693+
)
694+
self._is_stream_open = False
695+
await self.open()
696+
685697
await self.write_obj_stream.send(finalize_req)
686698
response = await self.write_obj_stream.recv()
687699
self.object_resource = response.resource
@@ -691,7 +703,19 @@ async def _do_finalize():
691703
try:
692704
return await retry_policy(_do_finalize)()
693705
finally:
694-
await self.write_obj_stream.close()
706+
if self.write_obj_stream:
707+
if self.write_obj_stream.is_stream_open:
708+
try:
709+
await self.write_obj_stream.close()
710+
except Exception as e:
711+
logger.warning(
712+
f"Error closing write stream during finalize cleanup. Got exception: {e}"
713+
)
714+
else:
715+
try:
716+
await self.write_obj_stream.close()
717+
except Exception:
718+
pass
695719
self._is_stream_open = False
696720
self.offset = None
697721

0 commit comments

Comments
 (0)