From aed1b466123135376a7c76c34101508e0e15d652 Mon Sep 17 00:00:00 2001 From: Slava Eliseev Date: Thu, 19 Dec 2024 11:08:25 +0300 Subject: [PATCH] Fix Windows PermissionError (WinError 5) while uploading datasets (#1349) --- clearml/binding/artifacts.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/clearml/binding/artifacts.py b/clearml/binding/artifacts.py index 093c80d7..2d04a80d 100644 --- a/clearml/binding/artifacts.py +++ b/clearml/binding/artifacts.py @@ -257,6 +257,7 @@ class Artifacts(object): max_preview_size_bytes = 65536 _flush_frequency_sec = 300. + _max_tmp_file_replace_attemps = 3 # notice these two should match _save_format = '.csv.gz' _compression = 'gzip' @@ -1138,7 +1139,21 @@ class Artifacts(object): temp_folder, prefix, suffix = self._temp_files_lookup.pop(local_filename) fd, temp_filename = mkstemp(prefix=prefix, suffix=suffix) os.close(fd) - os.replace(local_filename, temp_filename) + + for i in range(self._max_tmp_file_replace_attemps): + try: + os.replace(local_filename, temp_filename) + break + except PermissionError: + LoggerRoot.get_base_logger().warning( + "Failed to replace {} with {}. Attemps left: {}".format( + local_filename, temp_filename, self._max_tmp_file_replace_attemps - i + ) + ) + else: + # final attempt, and if it fails, throw an exception + # exception could be thrown on some Windows systems + os.replace(local_filename, temp_filename) local_filename = temp_filename os.rmdir(temp_folder) except Exception as ex: