Skip to content

The agent that grows with you

fix(update): a failed exclusive-create claim is withdrawn, never left torn and live (review F12)

Tekniumdd8f94f4b915
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.py
index e65c3c97b96..e985613d2c2 100644
--- a/hermes_cli/update_lock.py
+++ b/hermes_cli/update_lock.py
@@ -22,6 +22,7 @@ Two artifacts, one authority each:
from __future__ import annotations
import calendar
+import errno
import logging
import os
import re
@@ -1308,11 +1309,22 @@ def _publish_exclusive(path: Path, body: bytes) -> bool:
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0), 0o644)
except FileExistsError:
return False
+ made = os.fstat(fd)
try:
- os.write(fd, body)
+ if os.write(fd, body) != len(body):
+ raise OSError(errno.ENOSPC, f"short write publishing {path}")
os.fsync(fd)
- finally:
+ except BaseException:
+ # A torn claim names our live pid, so it would block every updater for as long as we
+ # run without our ever having acquired: withdraw the inode WE created — compared under
+ # the marker mutex, so a claimant that reclaimed it and published its own keeps that.
os.close(fd)
+ with suppress(OSError), marker_mutex(path):
+ now = os.stat(path)
+ if (now.st_dev, now.st_ino) == (made.st_dev, made.st_ino):
+ path.unlink()
+ raise
+ os.close(fd)
return True
diff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.py
index cb527390410..2b37532eef3 100644
--- a/tests/hermes_cli/test_update_lock.py
+++ b/tests/hermes_cli/test_update_lock.py
@@ -16,6 +16,7 @@ disk.
from __future__ import annotations
+import errno
import os
import subprocess
import sys
@@ -341,6 +342,51 @@ def test_tmp_litter_with_a_digit_lookalike_pid_never_breaks_admission(marker, ow
lock.release()
+def _torn_write(after=None):
+ """An ``os.write`` that lands part of the claim, then fails like a full disk."""
+ real_write = os.write
+
+ def torn(fd, data):
+ real_write(fd, data[: len(data) // 2])
+ if after is not None:
+ after()
+ raise OSError(errno.ENOSPC, "No space left on device")
+
+ return torn
+
+
+def _no_hard_links(*_args, **_kwargs):
+ raise PermissionError(errno.EPERM, "Operation not permitted")
+
+
+def test_failed_exclusive_create_leaves_no_torn_claim(marker, monkeypatch):
+ """Contract A3 without hard links (FAT, some network mounts): a claim whose write fails
+ part-way is withdrawn — a process that never acquired must not block every updater while
+ it lives."""
+ monkeypatch.setattr(os, "link", _no_hard_links)
+ monkeypatch.setattr(os, "write", _torn_write())
+ lock = UpdateLock(path=marker)
+ assert lock.acquire() is False
+ monkeypatch.undo()
+ assert not marker.exists()
+ assert read_live_update(path=marker) is None
+
+
[email protected]("posix") # replacing a file someone holds open needs POSIX unlink
+def test_withdrawing_a_torn_claim_never_deletes_a_replacement(marker, monkeypatch, other_pid):
+ """The withdrawal deletes only the inode it created: a claimant that reclaimed the torn
+ marker and published its own in between keeps its claim."""
+ def replace():
+ marker.unlink()
+ _claim_v2(marker, other_pid)
+
+ monkeypatch.setattr(os, "link", _no_hard_links)
+ monkeypatch.setattr(os, "write", _torn_write(replace))
+ assert UpdateLock(path=marker).acquire() is False
+ monkeypatch.undo()
+ assert marker.read_text(encoding="utf-8").startswith(f"{other_pid}\n")
+
+
def test_unreadable_creation_time_gets_the_v1_ceiling(marker, other_pid, monkeypatch):
"""Contract A1: a live pid whose creation time cannot be read (Windows denies it for
elevated/other-user pids) is live only within the legacy ceiling: it may be a reused pid."""