Skip to content

Commit 5281646

Browse files
Implement logic to identify offending processes on windows that are opening diplomat files...
1 parent 19f8a5a commit 5281646

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

diplomat/predictors/sfpe/file_io.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import BinaryIO, Tuple, Optional, Mapping, Any, Union, Callable
1010
from io import SEEK_END
1111
import numpy as np
12+
import psutil
1213
from typing_extensions import Protocol
1314
from importlib import import_module
1415
from 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

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ classifiers = [
2626
]
2727

2828
dependencies = [
29+
"numba", # Required, make sure this goes before numpy...
2930
"opencv-python-headless",
3031
"matplotlib",
3132
"typing_extensions>=3.8",
3233
"tqdm",
3334
"PyYAML",
3435
"pandas",
3536
"numpy",
36-
"numba",
37+
"psutil"
3738
]
3839
dynamic = ["version"]
3940

0 commit comments

Comments
 (0)