The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process): a spawn-ledger write past an unavailable lock is logged once at WARNING
kshitijk4poor3df6a4d5c41f
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.pyindex 54ebe4563c1..22d469b563b 100644--- a/hermes_cli/process_identity.py+++ b/hermes_cli/process_identity.py@@ -463,6 +463,7 @@ def _new_entry(# A ledger writer stopped mid-write (SIGSTOP, a debugger) must not stall every spawn_owned on the# machine: past this the write goes ahead under the thread lock alone, as when no lock can be taken._LEDGER_LOCK_TIMEOUT_S = 2.0+_ledger_lock_warned = Falsedef _append_entry(entry: LedgerEntry) -> bool:@@ -481,7 +482,13 @@ def _append_entry(entry: LedgerEntry) -> bool:try:held.enter_context(_FileLock(path.with_name(path.name + ".lock"), timeout=_LEDGER_LOCK_TIMEOUT_S))except (RuntimeError, OSError):- # Best-effort like the write itself: the thread lock is then the only guard.+ # Best-effort like the write itself: the thread lock is then the only guard, so a row another+ # Hermes process writes meanwhile can be lost. Say so once where it shows (agent.log).+ global _ledger_lock_warned+ if not _ledger_lock_warned:+ _ledger_lock_warned = True+ logger.warning("spawn ledger lock unavailable (a stalled holder, or no file locks here); writing "+ "without it, so concurrent Hermes processes may drop each other's rows")logger.debug("spawn ledger lock unavailable", exc_info=True)entries = _read_ledger_or_quarantine(path) or []# Drop malformed entries, our own stale entry, and provably dead pids.diff --git a/tests/hermes_cli/test_process_identity.py b/tests/hermes_cli/test_process_identity.pyindex 678c27468bd..952d83c46a8 100644--- a/tests/hermes_cli/test_process_identity.py+++ b/tests/hermes_cli/test_process_identity.py@@ -494,14 +494,18 @@ def test_concurrent_processes_never_drop_each_others_ledger_rows(tmp_path):@pytest.mark.skipif(os.name == "nt", reason="fcntl lock holder")-def test_a_stalled_ledger_lock_holder_never_blocks_register_child(tmp_path, monkeypatch):+def test_a_stalled_ledger_lock_holder_never_blocks_register_child(tmp_path, monkeypatch, caplog):"""A holder stopped mid-write (SIGSTOP, a debugger) kept the lock: every spawn_owned on the- machine then waited forever. Past the timeout the row is written under the thread lock alone."""+ machine then waited forever. Past the timeout the row is written under the thread lock alone, and+ that is logged once at WARNING (rows of concurrent writers can be lost meanwhile)."""import fcntl+ import loggingimport threadingmonkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))monkeypatch.setattr(pi, "_LEDGER_LOCK_TIMEOUT_S", 0.3)+ monkeypatch.setattr(pi, "_ledger_lock_warned", False)+ caplog.set_level(logging.DEBUG, logger=pi.logger.name)ledger = pi._ledger_path()ledger.parent.mkdir(parents=True, exist_ok=True)with open(ledger.with_name(ledger.name + ".lock"), "a+b") as holder:@@ -512,5 +516,8 @@ def test_a_stalled_ledger_lock_holder_never_blocks_register_child(tmp_path, monkwriter.start()writer.join(timeout=10)assert done == [True], "register_child waited on a stalled lock holder"+ assert pi.register_child(os.getpid(), "stalled-again") is True # a second write past the lock+ warnings = [r for r in caplog.records if r.levelno == logging.WARNING and "spawn ledger lock" in r.getMessage()]+ assert len(warnings) == 1, f"{len(warnings)} warnings for two writes past a stalled lock"rows = json.loads(ledger.read_text(encoding="utf-8"))- assert any(r.get("purpose") == "stalled-lock" for r in rows), "the row was not written past the timeout"+ assert any(r.get("purpose") == "stalled-again" for r in rows), "the row was not written past the timeout"