Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(update-lock): never report 'acquired' with no lock file on disk (#1524)
Felix-Ayushd0525648e56d
diff --git a/packages/core/src/repowise/core/update_lock.py b/packages/core/src/repowise/core/update_lock.pyindex e4f4e5660..a2d56f42c 100644--- a/packages/core/src/repowise/core/update_lock.py+++ b/packages/core/src/repowise/core/update_lock.py@@ -97,9 +97,24 @@ def try_acquire_update_lock(repo_path: Path, target_commit: str | None) -> dict[with contextlib.suppress(OSError):tmp_path.unlink(missing_ok=True)return None- # Lost the create race twice in a row: someone else just acquired a- # fresh lock — report it. A still-unreadable lock degrades to acquired.- return read_update_lock(repo_path)+ # Both create attempts lost a race against a stale lock that was then+ # unlinked. Rather than falling through to a bare ``read_update_lock``+ # — which can return ``None`` ("acquired") with *no lock file on disk*,+ # letting a caller proceed without holding the lock — make one final+ # exclusive create. Winner: return ``None`` (owned). Loser: report the+ # fresh winner. Still-unreadable degrades to acquired, as everywhere.+ try:+ lock_path.parent.mkdir(parents=True, exist_ok=True)+ tmp_path.write_text(data, encoding="utf-8")+ os.link(tmp_path, lock_path)+ except FileExistsError:+ return read_update_lock(repo_path)+ except OSError:+ return None+ finally:+ with contextlib.suppress(OSError):+ tmp_path.unlink(missing_ok=True)+ return Nonedef release_update_lock(repo_path: Path) -> None:diff --git a/tests/unit/cli/test_update_lock.py b/tests/unit/cli/test_update_lock.pyindex 125c6f89f..814f5dc2d 100644--- a/tests/unit/cli/test_update_lock.py+++ b/tests/unit/cli/test_update_lock.py@@ -85,6 +85,44 @@ def test_acquire_replaces_stale_lock(tmp_path: Path) -> None:assert payload["pid"] == os.getpid()+def test_loop_exhaustion_never_returns_acquired_without_a_lock_file(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Regression: after two lost create-races against a stale lock, the+ caller must not be handed ``None`` ("acquired") with no lock file on disk.++ Both initial ``os.link`` calls raise ``FileExistsError`` against a stale+ lock, exactly the interleaving that used to fall through to a bare+ ``read_update_lock`` and return ``None`` with nothing on disk — letting a+ concurrent update race on the same index. The function must make one final+ exclusive create so ``None`` always implies the caller owns a lock.+ """+ _write_lock(+ tmp_path,+ {"pid": _dead_pid(), "target_commit": "crashed", "started_at": time.time()},+ )++ real_link = os.link+ link_calls = 0++ def _failing_link(src: str | os.PathLike, dst: str | os.PathLike) -> None:+ nonlocal link_calls+ link_calls += 1+ if link_calls <= 2:+ raise FileExistsError(f"[Errno 17] File exists: '{dst}'")+ real_link(src, dst)++ monkeypatch.setattr(os, "link", _failing_link)++ assert helpers.try_acquire_update_lock(tmp_path, "fresh") is None+ assert (tmp_path / ".repowise" / ".update.lock").exists()+ payload = helpers.read_update_lock(tmp_path)+ assert payload is not None+ assert payload["target_commit"] == "fresh"+ assert payload["pid"] == os.getpid()+ assert link_calls == 3++def test_release_then_reacquire(tmp_path: Path) -> None:assert helpers.try_acquire_update_lock(tmp_path, "one") is Nonehelpers.release_update_lock(tmp_path)