fix: handle Windows PermissionError on directory fsync

On Windows, opening a directory with O_RDONLY raises PermissionError.
Wrap the directory fsync in a try/except PermissionError — NTFS journals
metadata synchronously so the directory sync is unnecessary there.

Also adjust test assertions to expect 1 fsync call (file only) on
Windows vs 2 (file + directory) on POSIX.
This commit is contained in:
hussein1362
2026-04-22 13:19:53 +08:00
committed by Xubin Ren
parent 512bf59b3c
commit 0932189860
2 changed files with 19 additions and 8 deletions
+10 -4
View File
@@ -296,11 +296,17 @@ class SessionManager:
if fsync:
# fsync the directory so the rename is durable.
fd = os.open(str(path.parent), os.O_RDONLY)
# On Windows, opening a directory with O_RDONLY raises
# PermissionError — skip the dir sync there (NTFS
# journals metadata synchronously).
try:
os.fsync(fd)
finally:
os.close(fd)
fd = os.open(str(path.parent), os.O_RDONLY)
try:
os.fsync(fd)
finally:
os.close(fd)
except PermissionError:
pass # Windows — directory fsync not supported
except BaseException:
tmp_path.unlink(missing_ok=True)
raise