Skip to content

The agent that grows with you

fix(pm): collect abandoned runtime leases (#125609, salvage #125632)

Ahmett101b23d4f4b32c5
diff --git a/hermes_cli/runtime_state.py b/hermes_cli/runtime_state.py
index 9e36b0dba1e..1cc75833dce 100644
--- a/hermes_cli/runtime_state.py
+++ b/hermes_cli/runtime_state.py
@@ -150,6 +150,7 @@ def lease_directory(generation: Path) -> Callable[[], None]:
return lambda: None # Generations produced before leases stay conservatively retained.
leases = generation / ".leases"
leases.mkdir(exist_ok=True)
+ _prune_unlocked_leases(leases)
lease = leases / uuid.uuid4().hex
fd = os.open(lease, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o600)
try:
@@ -199,11 +200,28 @@ def collect_generations(project: Path, *, min_age_seconds: float = 86400) -> lis
def leases_held(generation: Path) -> bool:
"""True while any process still holds a lease taken by ``lease_directory``."""
- for lease in (generation / ".leases").glob("*"):
- fd = os.open(lease, os.O_RDWR)
+ return _prune_unlocked_leases(generation / ".leases")
+
+
+def _prune_unlocked_leases(leases: Path) -> bool:
+ """Remove abandoned lease files and report whether any live lock remains.
+
+ Kernel locks disappear even when ``execv``, ``os._exit`` or a crash bypasses
+ ``atexit``. Cleaning those unlocked files whenever a reader arrives bounds leaks in
+ the selected generation too, which generation GC intentionally never visits.
+ """
+ held = False
+ for lease in leases.glob("*"):
+ try:
+ fd = os.open(lease, os.O_RDWR)
+ except FileNotFoundError:
+ continue
try:
if not _lock(fd, wait=False):
- return True
+ held = True
+ else:
+ with suppress(OSError):
+ lease.unlink()
finally:
os.close(fd)
- return False
+ return held
diff --git a/tests/pm/test_runtime_gc.py b/tests/pm/test_runtime_gc.py
index 66e8a2d9f10..02c94663695 100644
--- a/tests/pm/test_runtime_gc.py
+++ b/tests/pm/test_runtime_gc.py
@@ -1,6 +1,9 @@
"""Superseded and aborted PM runtime generations are collected; running workers are not."""
import json
+import os
from pathlib import Path
+import subprocess
+import sys
from pm.runtime import collect_runtime_generations
@@ -47,3 +50,32 @@ def test_collector_yields_to_an_in_flight_stage(tmp_path):
assert collect_runtime_generations(root) == []
assert aborted.is_dir()
assert collect_runtime_generations(root) == [aborted]
+
+
+def test_next_reader_removes_lease_left_by_hard_exit(tmp_path):
+ from hermes_cli.runtime_state import lease_directory
+
+ generation = _generation(tmp_path / "pm-runtime", "selected")
+ code = """
+import os
+import sys
+from pathlib import Path
+from hermes_cli.runtime_state import lease_directory
+
+lease_directory(Path(sys.argv[1]))
+os._exit(0)
+"""
+ subprocess.run([sys.executable, "-c", code, str(generation)], check=True, env=dict(os.environ))
+ leases = generation / ".leases"
+ stale = list(leases.iterdir())
+ assert len(stale) == 1
+
+ release = lease_directory(generation)
+ try:
+ active = list(leases.iterdir())
+ assert len(active) == 1
+ assert active[0] not in stale
+ finally:
+ release()
+
+ assert list(leases.iterdir()) == []