Skip to content

fix(agents): wait for hook state locks instead of failing with EAGAIN - #285

Merged
furedea merged 3 commits into
mainfrom
fix/hook-lock-wait
Oct 22, 2026
Merged

furedea merged 3 commits into
mainfrom
fix/hook-lock-wait

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

背景

フックの状態ロックを fcntl.flock(LOCK_NB) で取得していたため,別のフックがロックを保持していると即座に BlockingIOError(EAGAIN,macOS では errno 35)で失敗していました.

  • dotfiles#273: 成功した herdr の結果が 2 つ同時に PostToolUse へ届くと,後続のフックがレコードロックを取れず,所有権レコードが失われ,以後のプロンプトとクリーンアップが確認を求める.
  • agent-harness#169: サブエージェントを同時に起動すると PreToolUse が [Errno 35] Resource temporarily unavailable で失敗し,サブエージェントが何もせず終了する.

変更内容(最終的なロックの挙動)

file_lock は blocking=False(即時失敗),timeout=None(無期限に待機),timeout=秒(10 ms 間隔で再試行し,期限超過でロックのパスと another hook holds it を含む LockTimeoutError)を区別します.通常の待機上限 LOCK_WAIT_SECONDS は 0.5 秒で,呼び出し時に読み取ります.

箇所 挙動 理由
registered_record()(全フックの入口) ロックを取らない Stop が検証の間レコードロックを保持するため,同一セッションの他フックが失敗しないようにする.読み取りと touch は原子的書き込みで足りる
.register.lock(登録) 最大 0.5 秒の待機 登録の競合は短時間で終わる.既存レコードの reopen はこのロックを解放してから行い,待機が入れ子にならない
resume(reopen() のレコードロック) 最大 0.5 秒待ち,取れなければ touch のみして返る Stop の検証中でも SessionStart を失敗させない
herdr 所有権レコード(confirm) 専用の .herdr.lock で最大 0.5 秒の待機 レコードロックを使うと Stop の検証に待たされるため分離.同時の PostToolUse が両方とも記録される
Stop(gate) レコードロックを無期限に待機 検証の二重実行を直列化する従来の保証を維持
SessionEnd レコードロックを最大 2 秒待ち,取れなければ end() を省略 Codex の SessionEnd のタイムアウト 3 秒を超えない.検証中のセッションは mtime ベースの GC に任せる
GC .gc.lock 非ブロッキング.取れなければ GC を省略 GC は付随処理であり,他の GC が走っているだけで Stop や SessionEnd を失敗させない
GC のレコードごとのロック 非ブロッキング(保持中のレコードは飛ばす) Stop が長時間保持するため,レコード数に比例して待たない

テスト

  • 複数プロセスが同じセッションの hook エントリを同時に呼んでも EAGAIN で失敗しない.
  • 成功した herdr agent start / workspace create の結果が同時に PostToolUse へ届いても,実際のフックエントリ経由で両方のレコードが保持される.
  • 上限を超えてロックが保持された場合,ロックのパスと another hook holds it を含む明確なエラーになる.
  • Stop の検証中でも同一セッションの他フックが失敗しない.resume は touch のみで返り,SessionEnd は 2 秒で end() を省略する.GC が保持中のレコードや GC ロックを待たずに飛ばす.
  • uv run --frozen pytest tests/agents: 1973 passed.lefthook pre-commit: 全件成功.

Closes #273
Refs furedea/agent-harness#169

🤖 Generated with Claude Code

furedea and others added 3 commits October 22, 2026 11:49
Hook entries took the session record and registration locks with
LOCK_NB, so concurrent hooks failed at once with errno 35: a second
PostToolUse lost its herdr ownership record and simultaneous subagent
PreToolUse calls aborted. Wait up to 0.5 s (10 ms retries) and then fail
naming the lock and stating that another hook holds it. The limit keeps
three sequential acquisitions inside SessionEnd's 3 s timeout. GC keeps
skipping records held by a long Stop verification without waiting.

Closes #273
Refs furedea/agent-harness#169

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… lock waits

Address review findings on the bounded lock wait:

- GC takes .gc.lock without blocking and skips the pass when busy, so a
  concurrent GC can no longer fail Stop or SessionEnd.
- Stop (gate) and SessionEnd wait on the record lock without a limit
  again, restoring serialization of verification runs and session end.
- registered_record() no longer takes the record lock, so a long Stop
  verification cannot fail other hooks of the same session.
- herdr ownership records use their own bounded .herdr.lock instead of
  the record lock.
- Registration releases .register.lock before reopening an existing
  record, so the two bounded waits no longer nest.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…en busy

- SessionEnd waits up to 2 s for the record lock and then skips end(),
  leaving a busy session to mtime-based GC instead of exceeding the 3 s
  Codex hook timeout.
- Resume touches the record and returns when the record lock stays busy,
  rather than failing SessionStart.
- Raise LockTimeoutError for lock wait timeouts so callers can degrade
  on exactly that failure.
- Read LOCK_WAIT_SECONDS at call time so tests patch a single place.
- Restore the comment explaining the 0.5 s wait.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@furedea
furedea merged commit 04df07d into main Oct 22, 2026
11 checks passed
@furedea
furedea deleted the fix/hook-lock-wait branch October 22, 2026 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostToolUse の入口が非ブロッキングのロックで失敗し,並行した成功結果の記録を取りこぼす

1 participant