Repository navigation
Bound how long a dispatched hooks.d listener may run, and say when one is stopped (#286) - #290
Merged
Merged
Conversation
…n one is stopped dispatch() ran every listener sequentially in the foreground with nothing bounding it, so one that blocked stalled the agent for as long as it blocked and logged nothing — nothing had failed. The watchdog is a sibling process, not a poll: the hook is waited on directly so a fast one costs nothing, and after_post_tool dispatches on every tool call. The kill targets the hook's own PID and never its process group, because both shipped listeners put their git I/O in a disowned subshell and a group kill would leave a .git/index.lock in the store this plugin exists to protect. SIGTERM then SIGKILL after a grace period, so an EXIT trap can release a lock. A stopped hook is reported as a decline, not a finding: it never answered, and 143 is the signal we sent rather than its exit status. Budgets measured against the shipped listeners' foreground time (0.17-0.58s, including with the push remote blackholed): 15s where the agent waits, 120s where nothing does. 0 disables. Closes #286 Co-Authored-By: Max <noreply>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
dispatch()ran everyhooks.d/<event>/listener sequentially, in the foreground, with nothing bounding how long one may take. A listener that blocks — a network call with no timeout, areadon a pipe nobody writes to, a lock wait — held the agent for the length of the block and logged nothing, because nothing had failed.Ranking, up front and unchanged: no report, no observed stall. #286 was found by reading
dispatch()while fixing #280. This is a structural gap, not an incident.One finding that reshaped the design
The issue's two stated costs both rest on a premise I checked and found does not hold for the shipped hooks.
hooks.d/after_save/50-git-backup.shputs its entiregit add/commit/pushinside a disowned subshell () </dev/null >/dev/null 2>&1 &+disown) and thenexit 0s.50-git-restore.shdoes the same with its fetch.dispatch()never waits on any of it.Measured — the same fixture, with
originpointed at a blackholed address so the push hangs indefinitely:So "a backup hook doing a real
git pushover a slow network legitimately takes tens of seconds" is true of the push and false of the foreground, and the foreground is the only thing a dispatch timeout can see. That retires the hard half of the budget question, and it is most of the answer to Q4.The five questions
1. Per-hook budget, or one for the whole dispatch?
Per hook. A whole-dispatch budget makes a listener's fate depend on its neighbours' latency: the second listener gets killed for the first one's slowness, and the log line names a hook whose only fault was being later in the queue — unattributable, and exactly the "slow-but-honest listener spends the allowance of the five after it" the issue names. Worst case is stated in the README rather than capped: N listeners × budget.
Test:
test_one_slow_hook_does_not_spend_the_next_hook_s_allowance.2. Kill signal, and a
TERM→KILLgrace?SIGTERM, thenSIGKILLafterhooks.dispatch_kill_grace_seconds(5 s). Both premises measured rather than assumed:EXITtrap when it dies of an untrappedSIGTERM(exit 143) and does not onSIGKILL. Both shipped hooks release their lock from anEXITtrap on the no-flock(1)path, so the grace is the difference between a lock released and a lock file left holding a dead PID.git commitremoves its own.git/index.lockonSIGTERM— verified against agit commitheld open by a hangingpre-commithook. It cannot onSIGKILL.Courtesy, not veto: a listener that traps
TERMand keeps going is killed anyway, or the defect returns behind an opt-in (test_a_hook_that_ignores_TERM_is_still_stopped).3. Finding or decline?
Decline, reported as loudly as a failure. A hook that exits non-zero has answered — it ran, it failed, and #277 puts its status and its own first lines in the report. A killed hook has answered nothing, and
143is not its exit status, it is the signal we sent. Reporting the second in the shape of the first invents a verdict.So it is a separate function and a separate line, in both destinations, naming the hook, the event, which budget it exceeded, and that whether it did its work is UNKNOWN. The budget is in the line because otherwise a reader cannot tell a hung hook from a budget set too tight, and those want opposite fixes.
/remember:doctorshows it under "Recent errors" either way: a listener that hangs is a real problem with a real installation even when its own verdict is not knowable. Whatever it managed to say before it stopped is still relayed — that output is the only evidence of what it was doing.Tests:
test_the_stop_reaches_the_file_the_doctor_reads,test_a_timeout_is_reported_as_UNKNOWN_not_as_a_verdict_from_the_hook,test_what_a_hung_hook_managed_to_say_is_still_delivered.4. Does anything unwind the git-backup hook's partial state?
Nothing needs to, because nothing is interrupted — and the design is what makes that true.
The kill targets the hook's own PID, never its process group. Measured: a disowned grandchild survives a PID-targeted
TERM/KILLand dies with a group kill. Since both shipped hooks put every git write in exactly such a grandchild, a PID kill cannot land betweengit addandgit commit— the process holding the index is not the one being signalled.test_a_hook_s_detached_work_survives_the_killis the standing guard on that.For a third-party listener doing git work in the foreground,
SIGTERM+ grace hands it its ownEXITtrap and git's own lock cleanup (both measured above). Past that it is the hook's own responsibility, and the README now says so in those words.The price of choosing PID over group, stated rather than hidden: a listener blocked in a foreground child (a
curlwith no timeout) leaks that child when its parent dies. The stall ends, the process lingers. A leaked process is recoverable; a half-written index in someone's memory store is not.5. Are
after_user_prompt/after_session_startdifferent fromafter_save?Yes, and there is a structural reason, not just a feeling.
after_saveandafter_consolidateare dispatched fromsave-session.sh/run-consolidation.sh, whichpost-tool-hook.shstarts withnohup … &. Nobody is waiting. The other four are dispatched inside a Claude Code hook process the agent is blocked on.after_post_tool,after_user_prompt,before/after_session_startbefore/after_save,before/after_consolidatenohup'd; no host timeout and no one waiting. 15 s here would be a deadline nobody is keeping.Headroom against the measurements above: ~25× interactive, ~200× detached.
0disables; an unparseable value falls back to the shipped default rather than letting$(( ))decide underset -u(the #258 lesson).Implementation note: the healthy case stays free
A
kill -0poll on a one-second tick charges every hook a second it did not spend, andafter_post_tooldispatches on every tool call. So the hook is started in the background andwaited on directly — a hook returning in 40 ms costs 40 ms — with a sibling subshell sleeping out the budget beside it, cancelled the momentwaitreturns. Its stdio is pointed at/dev/nullso it cannot hold open the command substitutionuser-prompt-hook.shwraps the dispatch in.The empty-
hooks.d/case (the shipped distribution: a.gitkeepand nothing else) still costs nothing — not a process, not a file. Budget resolution is lazy on first executable hook, same discipline #230 applied toid -u.Not
timeout(1)— macOS does not ship it. That is the finding50-git-restore.shalready made for its detached fetch, generalised here rather than written twice, as the issue's related-work pattern predicted.Guard:
test_fast_hooks_are_not_made_slowercompares three trivial listeners against a zero-listener baseline rather than a fixed number.Red → green
RED, before the fix (
tests/test_dispatch_timeout_286.py):The 5 that passed pre-fix are the regression guards (fast hooks, healthy hook, failing hook,
0, garbage) — they pass vacuously before and meaningfully after.GREEN:
The file itself dropping from 263 s to 56 s is the fix, visible in the clock.
Full suite:
1287 passed, 43 skipped in 923.94s, coverage 94.16% (gate 80%).Docs
CHANGELOG.md— Fixed, with the measurements, the PID-vs-group decision and its price, and the decline-not-finding argument.README.md— three rows in the config table; two bullets in What ahooks.d/listener may say covering what a listener is given before it is stopped and that its detached work is deliberately left running.config.example.json— ahooksblock plus a_commentsentry.Closes #286