Skip to content

The agent that grows with you

fix(update): a Windows owner that cannot write the checkout-lock record refuses up front (review readonly-lock-windows-handoff)

Teknium838f43c254a7
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.py
index eb0fe8ffe62..a096eb0bbc5 100644
--- a/hermes_cli/update_lock.py
+++ b/hermes_cli/update_lock.py
@@ -1029,7 +1029,7 @@ def _lock_holder(fd_or_path) -> UpdateHolder:
def _open_lock_file(path: Path) -> tuple[int | None, object]:
"""``(fd, True)`` read-write; ``(fd, False)`` read-only for an existing lock file we may not
write (left root-owned by a ``sudo hermes update``: the kernel lock works on a read-only fd,
- contract A5); ``(None, reason)`` when neither opens."""
+ contract A5; POSIX only — a Windows owner needs its record); ``(None, reason)`` when neither opens."""
# Never follow a link planted at the name: the holder record is written into this file.
binary = getattr(os, "O_BINARY", 0) | getattr(os, "O_NOFOLLOW", 0)
try:
@@ -1082,6 +1082,14 @@ def _acquire_checkout(install_root: Path) -> UpdateHolder | None:
_unlock(fd, lease)
os.close(fd)
return _lock_holder(path)
+ if writable is not True and sys.platform == "win32":
+ # No fd inheritance on Windows: the update tree's children find their owner by
+ # this record (_held_by_our_windows_ancestor), so an owner that cannot write it
+ # would admit itself and then have its own children refused.
+ _unlock(fd)
+ os.close(fd)
+ return UpdateHolder(pid=0, age_seconds=0.0,
+ reason=f"{path} is read-only; delete it or fix its permissions")
if writable is True:
record = f"{os.getpid()}\n{int(time.time())}\n{_identity_line()}\n".encode()
os.lseek(fd, 0, os.SEEK_SET)
diff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.py
index 5beaa3c4b4b..7ebb3416655 100644
--- a/tests/hermes_cli/test_update_lock.py
+++ b/tests/hermes_cli/test_update_lock.py
@@ -867,6 +867,26 @@ def test_unwritable_lock_file_still_locks_the_checkout(tmp_path):
assert second.returncode == 3, "a second update ran while the read-only-locked checkout was held"
+def test_read_only_lock_file_is_refused_on_windows(tmp_path):
+ """Windows children cannot inherit the lock; they find their owner by the record in it. An
+ owner that can only open the lock file read-only refuses up front instead of admitting
+ itself and then having its own completion child refused."""
+ root = tmp_path / "install"
+ root.mkdir()
+ lock_file = checkout_lock_path(root)
+ lock_file.write_text("0\n0\n")
+ lock_file.chmod(0o444) # FILE_ATTRIBUTE_READONLY: O_RDWR is denied, the read-only open works
+ marker = tmp_path / "home" / ".hermes-update-in-progress"
+ try:
+ lock = UpdateLock(path=marker, install_root=root)
+ assert lock.acquire() is False
+ assert lock.holder is not None and lock.holder.reason
+ assert not marker.exists()
+ finally:
+ lock_file.chmod(0o666)
+
+
@pytest.mark.parametrize("link", [pytest.param("symlink", marks=pytest.mark.platforms("posix")), "link"])
def test_checkout_lock_never_writes_through_a_link_to_a_file_outside_the_install(tmp_path, link):
"""The holder record is written into the lock file itself: a symlink or hard link planted at