Skip to content

Commit f3a74bd

Browse files
committed
Auto-clean orphaned COW storage dirs from killed processes
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 56060f7 commit f3a74bd

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/sandlock/cowfs/_branch.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,24 @@ def finished(self) -> bool:
5050
return self._finished
5151

5252
def create(self) -> Path:
53-
"""Create the COW upper directory."""
53+
"""Create the COW upper directory.
54+
55+
Also cleans up any orphaned storage dirs from killed processes.
56+
"""
57+
# Clean stale dirs from previous crashed runs
58+
if self._storage.exists():
59+
for entry in self._storage.iterdir():
60+
if entry.is_dir():
61+
try:
62+
# Check if the owning process is still alive
63+
# Storage parent is /tmp/sandlock-cow-<pid>
64+
parts = self._storage.name.split("-")
65+
if len(parts) >= 3 and parts[-1].isdigit():
66+
pid = int(parts[-1])
67+
os.kill(pid, 0) # check if alive
68+
except (ProcessLookupError, ValueError):
69+
shutil.rmtree(str(entry), ignore_errors=True)
70+
5471
self._branch_id = uuid.uuid4().hex[:12]
5572
branch_dir = self._storage / self._branch_id
5673
branch_dir.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)