From 8771095a742f0dc767b26fc275244cebb214f21e Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:40:14 +0100 Subject: [PATCH 1/2] Bound task-state lock acquisition Architecture-Review: unchanged; task writers keep their existing lock and fail closed if it stays occupied. Co-Authored-By: Codex --- src/loopzero/cli.py | 10 +++++++++- tests/test_cli.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 29829078..4cc12c95 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -374,7 +374,15 @@ def _lock_task_state(wt: Path) -> Iterator[None]: directory.mkdir(parents=True, exist_ok=True) fd = os.open(directory, os.O_RDONLY | os.O_DIRECTORY) try: - fcntl.flock(fd, fcntl.LOCK_EX) + deadline = time.monotonic() + GIT_TIMEOUT + while (remaining := deadline - time.monotonic()) > 0: + try: + fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + break + except BlockingIOError: + _sleep(min(0.1, remaining)) + else: + raise TimeoutError(f"task state is busy in {wt}; retry when the other command finishes") yield finally: os.close(fd) diff --git a/tests/test_cli.py b/tests/test_cli.py index 53b0e0ba..fcdac4d5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4735,6 +4735,26 @@ def test_wait_rechecks_closed_unmerged_pr(monkeypatch, final) -> None: assert _closed_unmerged_wait(monkeypatch, reads, steps=1) == final +def test_task_state_lock_times_out_with_a_live_holder(tmp_path, monkeypatch): + now = 0.0 + + def advance(seconds): + nonlocal now + now += seconds + + monkeypatch.setattr(cli.time, "monotonic", lambda: now) + monkeypatch.setattr(cli, "_sleep", advance) + with cli._lock_task_state(tmp_path): + with ( + pytest.raises(TimeoutError, match=r"task state is busy.*retry"), + cli._lock_task_state(tmp_path), + ): + pytest.fail("contender acquired a held directory lock") + assert now == cli.GIT_TIMEOUT + with cli._lock_task_state(tmp_path): + assert now == cli.GIT_TIMEOUT + + def test_checks_report_survives_failed_publication(tmp_path, monkeypatch): path = tmp_path / cli.CHECKS_FILE monkeypatch.setattr(cli.worktree, "head", lambda _: next(heads)) From 9d17de4361b71af47572e553e045d73a7658f2e0 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:10:43 +0100 Subject: [PATCH 2/2] Isolate the task-state timeout test clock Co-Authored-By: Codex --- tests/test_cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index df26cd91..f9f62e24 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4706,8 +4706,7 @@ def advance(seconds): nonlocal now now += seconds - monkeypatch.setattr(cli.worktree.time, "monotonic", lambda: now) - monkeypatch.setattr(cli.worktree.time, "sleep", advance) + monkeypatch.setattr(cli.worktree, "time", SimpleNamespace(monotonic=lambda: now, sleep=advance)) with cli.worktree.lock_local_state(git_repo): with ( pytest.raises(TimeoutError, match=r"task state is busy.*retry"),