@@ -1156,8 +1156,10 @@ async def handle_data(data):
11561156 await original_handle_data (data )
11571157
11581158 async def finalize ():
1159+ nonlocal failed_download
11591160 if save_artifact and remote .policy != Remote .STREAMED :
11601161 await original_finalize ()
1162+ failed_download = False
11611163
11621164 downloader = remote .get_downloader (
11631165 remote_artifact = remote_artifact ,
@@ -1167,6 +1169,7 @@ async def finalize():
11671169 downloader .handle_data = handle_data
11681170 original_finalize = downloader .finalize
11691171 downloader .finalize = finalize
1172+ failed_download = True
11701173 try :
11711174 download_result = await downloader .run (
11721175 extra_data = {"disable_retry_list" : (DigestValidationError ,)}
@@ -1193,6 +1196,13 @@ async def finalize():
11931196 "Learn more on <https://pulpproject.org/pulpcore/docs/user/learn/"
11941197 "on-demand-downloading/#on-demand-and-streamed-limitations>"
11951198 )
1199+ finally :
1200+ if failed_download :
1201+ if downloader .path :
1202+ await sync_to_async (os .unlink )(downloader .path )
1203+
1204+ if hasattr (downloader , "session" ):
1205+ await downloader .session .close ()
11961206
11971207 if content_length := response .headers .get ("Content-Length" ):
11981208 self ._report_served_artifact_size (int (content_length ))
0 commit comments