fix(windows): append compute host logs without concurrent record loss
This commit is contained in:
1 parent
4115f1b4c0
commit
3f12c627ab
2 files changed
+53
-2
No files matched your search
@@ -55,14 +55,35 @@ _LATE_CONTROL_MAX = 64
|
||||
|
||||
|
||||
def append_log_record(path: str | Path, record: str) -> None:
|
||||
"""Append one log record using O_APPEND and exactly one os.write call."""
|
||||
"""Append a record with one native write and kernel-owned EOF placement."""
|
||||
p = Path(path)
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
text = record if record.endswith("\n") else f"{record}\n"
|
||||
data = text.encode("utf-8", errors="replace")
|
||||
if sys.platform == "win32":
|
||||
import ntsecuritycon
|
||||
import win32con
|
||||
import win32file
|
||||
|
||||
# The Windows CRT's O_APPEND seeks before writing, so independent file
|
||||
# descriptors can choose the same offset. Append-only access delegates
|
||||
# EOF placement to the kernel, without granting overwrite permission.
|
||||
handle = win32file.CreateFile(
|
||||
str(p), ntsecuritycon.FILE_APPEND_DATA,
|
||||
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
|
||||
None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None,
|
||||
)
|
||||
try:
|
||||
_, written = win32file.WriteFile(handle, data)
|
||||
if written != len(data):
|
||||
raise OSError("Incomplete log record write")
|
||||
finally:
|
||||
handle.Close()
|
||||
return
|
||||
fd = os.open(str(p), os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
|
||||
try:
|
||||
os.write(fd, data)
|
||||
if os.write(fd, data) != len(data):
|
||||
raise OSError("Incomplete log record write")
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
Reference in new issue
Block a user