@@ -244,13 +244,27 @@ def gzip_file_with_sufix(file_path: str, sufix: str) -> str | None:
244244 # Use pathlib for cleaner path operations
245245 renamed_dst = path_obj .with_name (f"{ path_obj .stem } _{ sufix } { path_obj .suffix } .gz" )
246246
247- try :
248- with open (file_path , "rb" ) as fin :
249- with gzip .open (renamed_dst , "wb" , compresslevel = 6 ) as fout : # Balanced compression
250- shutil .copyfileobj (fin , fout , length = 64 * 1024 ) # type: ignore # 64KB chunks for better performance
251- except (OSError , IOError ) as e :
252- write_stderr (f"Unable to gzip log file | { file_path } | { repr (e )} " )
253- raise e
247+ # Windows-specific retry mechanism for file locking issues
248+ max_retries = 3 if sys .platform == "win32" else 1
249+ retry_delay = 0.1 # 100ms delay between retries
250+
251+ for attempt in range (max_retries ):
252+ try :
253+ with open (file_path , "rb" ) as fin :
254+ with gzip .open (renamed_dst , "wb" , compresslevel = 6 ) as fout : # Balanced compression
255+ shutil .copyfileobj (fin , fout , length = 64 * 1024 ) # type: ignore # 64KB chunks for better performance
256+ break # Success, exit retry loop
257+ except PermissionError as e :
258+ # Windows file locking issue - retry with delay
259+ if attempt < max_retries - 1 and sys .platform == "win32" :
260+ time .sleep (retry_delay )
261+ continue
262+ # Final attempt failed or not Windows - treat as regular error
263+ write_stderr (f"Unable to gzip log file | { file_path } | { repr (e )} " )
264+ raise e
265+ except (OSError , IOError ) as e :
266+ write_stderr (f"Unable to gzip log file | { file_path } | { repr (e )} " )
267+ raise e
254268
255269 try :
256270 path_obj .unlink () # Use pathlib for deletion
0 commit comments