Skip to content

The agent that grows with you

fix(pm): say which lock a queued store writer is waiting on

ethernet053ec2854202
diff --git a/pm/store.py b/pm/store.py
index 7b4a69211b5..b775160c5b6 100644
--- a/pm/store.py
+++ b/pm/store.py
@@ -365,9 +365,14 @@ class Store:
"""Serialize writers using the same advisory lock as runtime publication."""
from hermes_cli.runtime_state import _lock
self.root.mkdir(parents=True, exist_ok=True)
- fd = os.open(self.root / ".install.lock", os.O_CREAT | os.O_RDWR, 0o600)
+ lock = self.root / ".install.lock"
+ fd = os.open(lock, os.O_CREAT | os.O_RDWR, 0o600)
try:
- _lock(fd, wait=True)
+ # A second `hermes pm install` behind an sdist build otherwise sits
+ # silent for minutes; say what it is waiting on.
+ if not _lock(fd, wait=True, timeout=2):
+ print(f"waiting for {lock} (another PM operation holds it)", file=sys.stderr, flush=True)
+ _lock(fd, wait=True)
yield
finally:
os.close(fd)
diff --git a/tests/pm/test_store_lock_wait.py b/tests/pm/test_store_lock_wait.py
new file mode 100644
index 00000000000..a47d21c4495
--- /dev/null
+++ b/tests/pm/test_store_lock_wait.py
@@ -0,0 +1,32 @@
+"""A writer queued behind the store lock says so instead of blocking silently."""
+import os
+import threading
+import time
+
+from hermes_cli.runtime_state import _lock
+from pm.store import Store
+
+
+def test_install_lock_reports_what_it_waits_on(tmp_path, capsys):
+ store = Store(tmp_path / "store")
+ store.root.mkdir()
+ holder = os.open(store.root / ".install.lock", os.O_CREAT | os.O_RDWR, 0o600)
+ assert _lock(holder, wait=False)
+ entered = threading.Event()
+
+ def contend():
+ with store.install_lock():
+ entered.set()
+
+ waiter = threading.Thread(target=contend, daemon=True)
+ waiter.start()
+ deadline = time.monotonic() + 15
+ err = ""
+ while "waiting for" not in err and time.monotonic() < deadline:
+ time.sleep(0.1)
+ err += capsys.readouterr().err
+ assert str(store.root / ".install.lock") in err
+ assert not entered.is_set()
+ os.close(holder)
+ assert entered.wait(timeout=10), "the message must not replace the wait"
+ waiter.join(timeout=5)