@@ -149,6 +149,7 @@ def download_target(self, target: Dict, destination_directory: str):
149149 """
150150
151151 temp_obj = None
152+ file_mirror_errors = {}
152153 file_mirrors = mirrors .get_list_of_mirrors (
153154 "target" , target ["filepath" ], self ._mirrors
154155 )
@@ -163,10 +164,20 @@ def download_target(self, target: Dict, destination_directory: str):
163164 _check_hashes_obj (temp_obj , target ["fileinfo" ]["hashes" ])
164165 break
165166
166- except Exception as exception :
167+ except Exception as exception : # pylint: disable=broad-except
168+ # Store the exceptions until all mirrors are iterated.
169+ # If an exception is raised from one mirror but a valid
170+ # file is found in the next one, the first exception is ignored.
171+ file_mirror_errors [file_mirror ] = exception
172+
167173 if temp_obj :
168174 temp_obj .close ()
169- raise exceptions .NoWorkingMirrorError ({file_mirror : exception })
175+ temp_obj = None
176+
177+ # If all mirrors are iterated but a file object is not successfully
178+ # downloaded and verifies, raise the collected errors
179+ if not temp_obj :
180+ raise exceptions .NoWorkingMirrorError (file_mirror_errors )
170181
171182 filepath = os .path .join (destination_directory , target ["filepath" ])
172183 sslib_util .persist_temp_file (temp_obj , filepath )
@@ -314,6 +325,7 @@ def _root_mirrors_download(self, root_mirrors: Dict) -> "RootWrapper":
314325 finally :
315326 if temp_obj :
316327 temp_obj .close ()
328+ temp_obj = None
317329
318330 if not intermediate_root :
319331 # If all mirrors are tried but a valid root file is not found,
@@ -353,6 +365,7 @@ def _load_timestamp(self) -> None:
353365 finally :
354366 if temp_obj :
355367 temp_obj .close ()
368+ temp_obj = None
356369
357370 if not verified_timestamp :
358371 raise exceptions .NoWorkingMirrorError (file_mirror_errors )
@@ -406,6 +419,7 @@ def _load_snapshot(self) -> None:
406419 finally :
407420 if temp_obj :
408421 temp_obj .close ()
422+ temp_obj = None
409423
410424 if not verified_snapshot :
411425 raise exceptions .NoWorkingMirrorError (file_mirror_errors )
@@ -461,6 +475,7 @@ def _load_targets(self, targets_role: str, parent_role: str) -> None:
461475 finally :
462476 if temp_obj :
463477 temp_obj .close ()
478+ temp_obj = None
464479
465480 if not verified_targets :
466481 raise exceptions .NoWorkingMirrorError (file_mirror_errors )
0 commit comments