Skip to content

The agent that grows with you

fix(update): pause-record temp cleanup, per-thread mutex, corrupt history quarantine

Teknium183eff2f5d3d
diff --git a/hermes_cli/update_pause_record.py b/hermes_cli/update_pause_record.py
index cc113894ba0..a225adbba33 100644
--- a/hermes_cli/update_pause_record.py
+++ b/hermes_cli/update_pause_record.py
@@ -31,6 +31,8 @@ unlinks its files: an unlink Windows refuses (a reader holding the file without
FILE_SHARE_DELETE) leaves a copy that is redundant by id, never an obligation that executes again.
The list is read only under the mutex, and only its absence means "nothing retired": a list that
cannot be read means unknown, so nothing is claimed and the list is never rewritten from that read.
+A list that reads but does not parse is unknown too while any record or claim exists; with none
+left it guards nothing and is set aside as ``<record stem>.corrupt``.
"""
from __future__ import annotations
@@ -41,6 +43,7 @@ import json
import os
import subprocess
import sys
+import threading
import time
import uuid
from contextlib import contextmanager, suppress
@@ -49,7 +52,9 @@ from pathlib import Path
RECORD_STEM = ".hermes-update-paused-gateways"
MUTEX_NAME = RECORD_STEM + ".lock"
_MUTEX_WAIT_S = 10.0
-_mutex_depth = 0
+# Re-entry depth per THREAD: another thread of this process (a gateway's consumer, a watcher)
+# must wait for the kernel lock like any other holder, never ride on this thread's hold.
+_mutex_held = threading.local()
def install_root() -> Path:
@@ -102,14 +107,15 @@ class RetiredUnknown(OSError):
@contextmanager
def _mutex(wait_s: float = _MUTEX_WAIT_S):
- """Exclusive kernel lock on the record directory's sidecar (A7). Re-entrant in-process."""
- global _mutex_depth
- if _mutex_depth:
- _mutex_depth += 1
+ """Exclusive kernel lock on the record directory's sidecar (A7). Re-entrant per thread; each
+ thread's first entry opens its own descriptor, so threads exclude each other like processes."""
+ depth = getattr(_mutex_held, "depth", 0)
+ if depth:
+ _mutex_held.depth = depth + 1
try:
yield
finally:
- _mutex_depth -= 1
+ _mutex_held.depth -= 1
return
from hermes_cli import update_lock
path = record_path().with_name(MUTEX_NAME)
@@ -119,13 +125,13 @@ def _mutex(wait_s: float = _MUTEX_WAIT_S):
deadline = time.monotonic() + wait_s
while not update_lock._try_lock(fd):
if time.monotonic() > deadline:
- raise RecordBusy(f"{path} is held by another process")
+ raise RecordBusy(f"{path} is held by another process or thread")
time.sleep(0.05)
- _mutex_depth = 1
+ _mutex_held.depth = 1
try:
yield
finally:
- _mutex_depth = 0
+ _mutex_held.depth = 0
update_lock._unlock(fd)
finally:
os.close(fd)
@@ -216,11 +222,18 @@ def tree_is_whole(token: dict, root: Path | None = None) -> tuple[bool, str]:
def _atomic_write(path: Path, body: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_name(f"{path.name}.{os.getpid()}.tmp")
- with open(tmp, "w", encoding="utf-8") as fh:
- json.dump(body, fh, indent=1, sort_keys=True)
- fh.flush()
- os.fsync(fh.fileno())
- os.replace(tmp, path)
+ try:
+ with open(tmp, "w", encoding="utf-8") as fh:
+ json.dump(body, fh, indent=1, sort_keys=True)
+ fh.flush()
+ os.fsync(fh.fileno())
+ os.replace(tmp, path)
+ except BaseException:
+ # A refused replace (a Windows reader without FILE_SHARE_DELETE) or a failed write must
+ # not leave the half-published temp beside the record; the error itself still propagates.
+ with suppress(OSError):
+ tmp.unlink()
+ raise
def read(path: Path | None = None) -> dict | None:
@@ -341,16 +354,36 @@ def _retired(path: Path) -> set[str]:
claimed again and the list is never rewritten from a read that failed."""
target = _retired_path(path)
try:
- ids = json.loads(target.read_text(encoding="utf-8-sig")).get("ids")
+ text = target.read_text(encoding="utf-8-sig", errors="replace")
except FileNotFoundError:
return set()
- except (OSError, ValueError, AttributeError) as exc:
+ except OSError as exc: # a refused read (sharing violation, AV scanner): unknown
raise RetiredUnknown(f"cannot read the retired paused-gateway list {target}: {exc}") from exc
+ try:
+ ids = json.loads(text).get("ids")
+ except (ValueError, AttributeError): # not JSON / not an object: malformed, like a non-list
+ ids = None
if not isinstance(ids, list):
- raise RetiredUnknown(f"the retired paused-gateway list {target} is malformed")
+ return _quarantine_retired(path, target)
return {str(i) for i in ids}
+def _quarantine_retired(path: Path, target: Path) -> set[str]:
+ """A retired list that reads but does not parse. While a record or claim of this checkout
+ exists the list may be the only proof it is complete: unknown, fail closed. With none left it
+ guards nothing, and keeping it would abort every Windows update: set it aside and go on."""
+ if path.exists() or _claims(path):
+ raise RetiredUnknown(f"the retired paused-gateway list {target} is malformed")
+ corrupt = target.with_suffix(".corrupt")
+ try:
+ os.replace(target, corrupt)
+ except OSError as exc:
+ raise RetiredUnknown(f"cannot set aside the malformed retired paused-gateway list {target}: {exc}") from exc
+ print(f" ⚠ Set aside a malformed paused-gateway history (no paused set refers to it): {corrupt}",
+ file=sys.stderr)
+ return set()
+
+
def _retire(path: Path, carriers: list[tuple[Path, dict]]) -> None:
"""Complete the obligations *carriers* hold (call under the mutex): their ids (and every id
they absorbed) go on the durable retired list FIRST; a failed read or write raises and deletes
diff --git a/tests/hermes_cli/test_update_pause_record.py b/tests/hermes_cli/test_update_pause_record.py
index 7353f5389ef..4d2606bcd41 100644
--- a/tests/hermes_cli/test_update_pause_record.py
+++ b/tests/hermes_cli/test_update_pause_record.py
@@ -596,3 +596,57 @@ def test_an_unreadable_retired_list_claims_nothing_and_forgets_nothing(tmp_path,
retired.chmod(0o644)
assert json.loads(retired.read_text(encoding="utf-8-sig"))["ids"] == [done], "the history was rewritten"
assert done not in owed()
+
+
+
+# --- Review W3: record hygiene --------------------------------------------------------------------
+def test_a_refused_publish_leaves_no_temp_beside_the_record(tmp_path, monkeypatch):
+ target = tmp_path / "records" / "record.json"
+
+ def refuse(src, dst):
+ raise PermissionError(13, "Access is denied", str(dst)) # a reader without FILE_SHARE_DELETE
+
+ monkeypatch.setattr(pause_record.os, "replace", refuse)
+ with pytest.raises(PermissionError):
+ pause_record._atomic_write(target, {"schema": 1})
+ assert sorted(p.name for p in target.parent.iterdir()) == [], "the half-published temp was left behind"
+
+
+def test_the_record_mutex_excludes_other_threads_of_this_process(tmp_path, monkeypatch):
+ import threading
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ held, release = threading.Event(), threading.Event()
+
+ def holder():
+ with pause_record._mutex():
+ held.set()
+ release.wait(30)
+
+ thread = threading.Thread(target=holder)
+ thread.start()
+ try:
+ assert held.wait(30)
+ with pytest.raises(pause_record.RecordBusy), pause_record._mutex(wait_s=0.3):
+ pass # rode on the other thread's hold
+ finally:
+ release.set()
+ thread.join(30)
+ with pause_record._mutex(), pause_record._mutex(): # re-entry on one thread still works
+ pass
+
+
+def test_a_malformed_retired_list_is_set_aside_only_when_nothing_refers_to_it(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ retired = pause_record._retired_path(pause_record.record_path())
+ retired.write_text("{trunc", encoding="utf-8")
+ assert pause_record.adopt_orphans() == (None, []), "a corrupt history aborted the update"
+ assert not retired.exists() and retired.with_suffix(".corrupt").read_text(encoding="utf-8-sig") == "{trunc"
+
+ # With a record on disk the list may be the only proof it was completed: still unknown.
+ pause_record.write({"pause_id": "c" * 32, "resume_needed": True, "profiles": {"default": 4242}},
+ owner=pause_record.UNOWNED)
+ retired.write_text("{trunc", encoding="utf-8")
+ with pytest.raises(pause_record.RetiredUnknown):
+ pause_record.orphans()
+ assert retired.read_text(encoding="utf-8-sig") == "{trunc"