Skip to content

fix: avoid index.lock contention when polling repository status - #37

Merged
ryota-kishimoto merged 2 commits into
mainfrom
fix/no-optional-locks
Jul 31, 2026
Merged

ryota-kishimoto merged 2 commits into
mainfrom
fix/no-optional-locks

Conversation

@ryota-kishimoto

Copy link
Copy Markdown
Owner

概要

30秒ごとの監視で走る git status が、ユーザー自身の git 操作を
fatal: Unable to create '.../.git/index.lock': File exists で失敗させることがある。

git status はワーキングツリー走査で古くなった stat 情報を見つけると、
index を書き戻すために .git/index.lock を取る(読み取り専用に見えて実際は書く)。
監視対象4リポジトリ × 30秒間隔でこれが走るため、ユーザーが git checkout や
git commit した瞬間にロックが衝突する。

再現

400ファイルのリポジトリで、git status 5並列に git checkout をぶつけた結果:

status の実行方法 checkout の失敗率
git status --porcelain(現行) 9 / 60
git --no-optional-locks status --porcelain 0 / 60

--no-optional-locks は「index 更新は任意」と git に伝えるフラグで、まさにこの用途
(監視ツールが index を書かないようにする)のために用意されている。

なお git status 側は index リフレッシュを best-effort 扱いにしており、
ロックが取れなくても黙って握り潰して exit 0 で終わる。つまり被害を受けるのは
常にユーザー側の git コマンド
で、GitPeek 側のログには何も残らない。

変更内容

  • getStatus を git --no-optional-locks status --porcelain に変更。
    監視ループが .git/index を書き換えなくなる
  • updateRepository の fetch を detach された Task {} から await に変更。
    誰も待たないため前サイクルの fetch が次サイクルと重なり得た

branch --show-current / rev-parse / remote get-url / worktree list は
index を書かないことを確認済みのため、そのまま。

テスト

  • testGetStatus_doesNotRewriteIndex を追加。stat 情報を意図的に古くしてから
    getStatus を呼び、.git/index の mtime が変わらないことを検証する
    (修正前は fail、修正後は pass を確認)
  • 既存の unit テストは全て pass(38件)
  • ViewSnapshotTests の4件は main でも同様に失敗する既存の失敗で、本 PR とは無関係

getStatus currently runs plain 'git status --porcelain', which refreshes
stale stat information by rewriting .git/index. Assert that the index is
left untouched so the monitoring path cannot compete for index.lock.
Run status with --no-optional-locks so the 30s monitor never rewrites
.git/index, and await the optional fetch instead of detaching it into an
unawaited Task so a slow fetch cannot overlap the next refresh cycle.
@github-actions

Copy link
Copy Markdown

⚠️ このPRには関連するIssueがリンクされていません。

PR本文に Fixes #123 のような形式でIssue番号を追加するか、Issueが不要な場合はPRタイトルに [no-issue] を追加してください。

@ryota-kishimoto
ryota-kishimoto merged commit 982bc0c into main Jul 31, 2026
17 checks passed
@ryota-kishimoto
ryota-kishimoto deleted the fix/no-optional-locks branch July 31, 2026 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant