Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/loopzero/worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import stat
import subprocess
import time
from collections.abc import Iterator
from contextlib import contextmanager, suppress
from pathlib import Path
Expand Down Expand Up @@ -111,7 +112,15 @@ def _local_state_directory(wt: Path, *, create: bool = False, lock: bool = False
raise WorktreeError(f"local state directory is not a directory or is a symbolic link: {path}") from exc
try:
if lock:
fcntl.flock(fd, fcntl.LOCK_EX)
deadline = time.monotonic() + 60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Lock budget is a bare 60 instead of a named operation budget

The task says the lock should reuse the existing 60-second operation budget. Before the merge, the code tied it to cli.GIT_TIMEOUT. Now _local_state_directory hardcodes 60, and the test asserts the literal now == 60. If cli.GIT_TIMEOUT (or sandbox.GIT_TIMEOUT) changes, the lock budget will silently stop matching it. worktree cannot import cli, so the smallest fix is a module constant such as LOCK_TIMEOUT = 60.0 in worktree.py, used in both the loop and the test (now == cli.worktree.LOCK_TIMEOUT).

while (remaining := deadline - time.monotonic()) > 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Lock retry loop duplicates the poll-until-deadline pattern

This adds another hand-written loop that waits with time.sleep(min(step, remaining)) until a deadline, alongside the ones in cli.py (~1417) and candidate.py (~499). It behaves correctly. On timeout it raises TimeoutError, which is an OSError, so the best-effort paths at cli.py:584 (PR checks refresh) and cli.py:872 (review summary) still just print a note. Publication at cli.py:709 propagates the error to the operator. A guarded write_local_state (where expected_head is set) also writes nothing on timeout. This is non-blocking. Consider moving the loop into a small _flock_with_deadline(fd, budget) helper, so _local_state_directory stays focused on directory validation.

try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except BlockingIOError:
time.sleep(min(0.1, remaining))
else:
raise TimeoutError(f"task state is busy in {wt}; retry when the other command finishes")
yield fd
finally:
os.close(fd)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4699,6 +4699,25 @@ 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(git_repo, monkeypatch):
now = 0.0

def advance(seconds):
nonlocal now
now += seconds

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"),
cli.worktree.lock_local_state(git_repo),
):
pytest.fail("contender acquired a held directory lock")
assert now == 60
with cli.worktree.lock_local_state(git_repo):
assert now == 60


@pytest.mark.parametrize("redirected", [False, True])
def test_checks_report_survives_failed_publication(git_repo, tmp_path, monkeypatch, redirected):
path = git_repo / cli.CHECKS_FILE
Expand Down
Loading