Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

qualify locks: wait in the kernel flock queue instead of polling (a poller starves behind blocking waiters)

YanissAmz994261ad7fd0
diff --git a/tools/qualify/locks.py b/tools/qualify/locks.py
index 6337e33..0c62241 100644
--- a/tools/qualify/locks.py
+++ b/tools/qualify/locks.py
@@ -4,29 +4,43 @@ from __future__ import annotations
import contextlib
import fcntl
import os
+import signal
import time
MIN_MEM_GIB = 4.0
+def _blocking_flock(fd: int, wait_s: float, path: str) -> None:
+ def on_alarm(*_):
+ raise TimeoutError(f"lock {path} not free after {wait_s:.0f} s")
+ old = signal.signal(signal.SIGALRM, on_alarm)
+ signal.alarm(int(wait_s))
+ try:
+ fcntl.flock(fd, fcntl.LOCK_EX)
+ except BaseException:
+ os.close(fd)
+ raise
+ finally:
+ signal.alarm(0)
+ signal.signal(signal.SIGALRM, old)
+
+
@contextlib.contextmanager
def gpu_locks(paths: list[str], log=print, wait_s: float = 6 * 3600):
- """Take every lock in order (flock, polling so a wait can be reported); release in reverse order."""
+ """Take every lock in order (flock; a busy lock is waited on in the kernel queue); release in reverse order."""
fds = []
try:
for p in paths:
os.makedirs(os.path.dirname(p), exist_ok=True)
fd = os.open(p, os.O_RDWR | os.O_CREAT, 0o644)
- t0 = time.time(); said = False
- while True:
- try:
- fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB); break
- except BlockingIOError:
- if time.time() - t0 > wait_s:
- os.close(fd); raise TimeoutError(f"lock {p} not free after {wait_s:.0f} s")
- if not said:
- log(f"waiting for lock {p}"); said = True
- time.sleep(5)
+ t0 = time.time()
+ try:
+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ except BlockingIOError:
+ # Blocking wait (kernel queue), not a poll: a poller loses every release to the `flock -w` waiters queued
+ # beside it (autotune0 sat 4 h behind blocking waiters with a 5 s poll)
+ log(f"waiting for lock {p}")
+ _blocking_flock(fd, wait_s, p)
fds.append(fd)
log(f"lock held {p} (waited {time.time()-t0:.0f} s)")
yield