99from typing import BinaryIO , Tuple , Optional , Mapping , Any , Union , Callable
1010from io import SEEK_END
1111import numpy as np
12+ import psutil
1213from typing_extensions import Protocol
1314from importlib import import_module
1415from diplomat .predictors .fpe .sparse_storage import ForwardBackwardFrame
@@ -603,6 +604,26 @@ def flush(self):
603604 if not self ._file .closed :
604605 self ._file .flush ()
605606
607+ def _check_for_offending_processes (self , path : Union [str , Path ]):
608+ path = Path (path ).resolve ()
609+ offending_procs = []
610+
611+ for pid in psutil .pids ():
612+ try :
613+ proc = psutil .Process (pid )
614+
615+ for file in proc .open_files ():
616+ p = Path (file .path ).resolve ()
617+ if p == path :
618+ offending_procs .append (proc )
619+ break
620+ except Exception as e :
621+ pass
622+
623+ for proc in offending_procs :
624+ print (f"Process { proc .name ()} has diplomat file { path } open..." )
625+
626+
606627 def commit (self ):
607628 # Copy the file over...
608629 shutil .copy (self ._scratch_path , self ._commiter_path )
@@ -611,15 +632,18 @@ def commit(self):
611632 os .replace (self ._commiter_path , self ._final_path )
612633 except PermissionError as e :
613634 print (f"Failed to replace the file due to { repr (e )} , running fallback method..." )
635+ self ._check_for_offending_processes (self ._final_path )
614636 # Windows does some weird stuff here... We fallback to the 'unsafe' option...
615637 try :
616638 os .remove (self ._final_path )
617639 os .rename (self ._commiter_path , self ._final_path )
618640 except (FileNotFoundError , PermissionError ) as e :
619- print (f"Unable to remove the file due to { e } " )
641+ # The above can still fail on windows...
642+ print (f"Unable to move the file with fallback due to { repr (e )} , copying instead..." )
643+ if isinstance (e , PermissionError ):
644+ self ._check_for_offending_processes (self ._final_path )
620645 shutil .copyfile (self ._commiter_path , self ._final_path )
621646
622-
623647 # Reset edit info...
624648 self ._last_flush = time .monotonic ()
625649 self ._edit_count = 0
0 commit comments