Skip to content

Commit 7c66347

Browse files
committed
Add fsync on commit to prevent data loss on crash
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent f3a74bd commit 7c66347

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/sandlock/cowfs/_branch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def commit(self) -> None:
137137
dest.unlink()
138138

139139
# Copy files from upper to target
140+
synced_dirs = set()
140141
for root, dirs, files in os.walk(upper):
141142
rel = os.path.relpath(root, upper)
142143
for d in dirs:
@@ -147,6 +148,17 @@ def commit(self) -> None:
147148
dest = target / rel / f
148149
dest.parent.mkdir(parents=True, exist_ok=True)
149150
shutil.copy2(str(src), str(dest))
151+
synced_dirs.add(str(dest.parent))
152+
153+
# fsync all modified directories to ensure data is on disk
154+
# before removing the upper dir
155+
for d in synced_dirs:
156+
try:
157+
fd = os.open(d, os.O_RDONLY | os.O_DIRECTORY)
158+
os.fsync(fd)
159+
os.close(fd)
160+
except OSError:
161+
pass
150162

151163
cleanup_branch_dir(self._storage, self._branch_id)
152164
self._finished = True

0 commit comments

Comments
 (0)