The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(process): the bounded file lock reuses pm.filesystem.lock_fd and retries only on contention
kshitijk4poor63e1c2231dec
diff --git a/hermes_cli/active_sessions.py b/hermes_cli/active_sessions.pyindex 39da4eae334..6202d46cc87 100644--- a/hermes_cli/active_sessions.py+++ b/hermes_cli/active_sessions.py@@ -10,6 +10,7 @@ from __future__ import annotationsimport jsonimport loggingimport collections+import errnoimport mathimport osimport threading@@ -196,32 +197,21 @@ def _lease_paths(def _flock(fh, *, lock: bool, timeout: Optional[float] = None) -> None:- """Exclusive whole-file lock/unlock on ``fh`` (fcntl on POSIX, msvcrt on Windows). With- ``timeout`` the lock is polled non-blocking and OSError raised once it passes, so a holder that is- stopped mid-write (SIGSTOP, a debugger) cannot stall the caller forever."""+ """Exclusive whole-file lock/unlock on ``fh`` (fcntl on POSIX, msvcrt on Windows). With ``timeout``+ the lock is retried only while another holder has it, then BlockingIOError is raised, so a holder+ stopped mid-write (SIGSTOP, a debugger) cannot stall the caller; other errors raise at once."""+ if lock and timeout is not None:+ from pm.filesystem import lock_fd+ if not lock_fd(fh.fileno(), wait=True, timeout=timeout):+ raise BlockingIOError(errno.EWOULDBLOCK, f"file lock still held after {timeout}s")+ returnif os.name == "nt":import msvcrtfh.seek(0)+ msvcrt.locking(fh.fileno(), msvcrt.LK_LOCK if lock else msvcrt.LK_UNLCK, 1)else:import fcntl- if not lock:- if os.name == "nt":- msvcrt.locking(fh.fileno(), msvcrt.LK_UNLCK, 1)- else:- fcntl.flock(fh.fileno(), fcntl.LOCK_UN)- return- deadline = None if timeout is None else time.monotonic() + timeout- while True:- try:- if os.name == "nt":- msvcrt.locking(fh.fileno(), msvcrt.LK_LOCK if deadline is None else msvcrt.LK_NBLCK, 1)- else:- fcntl.flock(fh.fileno(), fcntl.LOCK_EX if deadline is None else fcntl.LOCK_EX | fcntl.LOCK_NB)- return- except OSError:- if deadline is None or time.monotonic() >= deadline:- raise- time.sleep(0.02)+ fcntl.flock(fh.fileno(), fcntl.LOCK_EX if lock else fcntl.LOCK_UN)class _FileLock:@@ -238,7 +228,7 @@ class _FileLock:except Exception as exc:self._fh.close()self._fh = None- raise RuntimeError("active session file lock unavailable") from exc+ raise RuntimeError(f"file lock unavailable: {self.path}") from excreturn selfdef __exit__(self, exc_type, exc, tb):diff --git a/tests/hermes_cli/test_process_identity.py b/tests/hermes_cli/test_process_identity.pyindex b04ded41107..678c27468bd 100644--- a/tests/hermes_cli/test_process_identity.py+++ b/tests/hermes_cli/test_process_identity.py@@ -494,7 +494,7 @@ 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_a_spawn(tmp_path, monkeypatch):+def test_a_stalled_ledger_lock_holder_never_blocks_register_child(tmp_path, monkeypatch):"""A holder stopped mid-write (SIGSTOP, a debugger) kept the lock: every spawn_owned on themachine then waited forever. Past the timeout the row is written under the thread lock alone."""import fcntl@@ -512,3 +512,5 @@ def test_a_stalled_ledger_lock_holder_never_blocks_a_spawn(tmp_path, monkeypatchwriter.start()writer.join(timeout=10)assert done == [True], "register_child waited on a stalled lock holder"+ 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"