File tree Expand file tree Collapse file tree
packages/google-cloud-storage/google/cloud/storage/asyncio Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments