Skip to content

Bound how long a dispatched hooks.d listener may run, and say when one is stopped (#286) - #290

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/286-dispatch-timeout
Aug 1, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/286-dispatch-timeout

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

dispatch() ran every hooks.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, a read on 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.sh puts its entire git add / commit / push inside a disowned subshell () </dev/null >/dev/null 2>&1 & + disown) and then exit 0s. 50-git-restore.sh does the same with its fetch. dispatch() never waits on any of it.

Measured — the same fixture, with origin pointed at a blackholed address so the push hangs indefinitely:

after_save/50-git-backup.sh, reachable remote        0.46 – 0.49 s
after_save/50-git-backup.sh, BLACKHOLED remote       0.51 – 0.58 s
before_session_start/50-git-restore.sh               0.17 – 0.22 s
a bare hook that only sources log.sh                 0.12 – 0.17 s

So "a backup hook doing a real git push over 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→KILL grace?

SIGTERM, then SIGKILL after hooks.dispatch_kill_grace_seconds (5 s). Both premises measured rather than assumed:

  • bash runs an EXIT trap when it dies of an untrapped SIGTERM (exit 143) and does not on SIGKILL. Both shipped hooks release their lock from an EXIT trap 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 commit removes its own .git/index.lock on SIGTERM — verified against a git commit held open by a hanging pre-commit hook. It cannot on SIGKILL.

Courtesy, not veto: a listener that traps TERM and 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 143 is 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:doctor shows 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/KILL and dies with a group kill. Since both shipped hooks put every git write in exactly such a grandchild, a PID kill cannot land between git add and git commit — the process holding the index is not the one being signalled. test_a_hook_s_detached_work_survives_the_kill is the standing guard on that.

For a third-party listener doing git work in the foreground, SIGTERM + grace hands it its own EXIT trap 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 curl with 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_start different from after_save?

Yes, and there is a structural reason, not just a feeling. after_save and after_consolidate are dispatched from save-session.sh / run-consolidation.sh, which post-tool-hook.sh starts with nohup … &. Nobody is waiting. The other four are dispatched inside a Claude Code hook process the agent is blocked on.

budget why
after_post_tool, after_user_prompt, before/after_session_start 15 s A human has pressed enter. Claude Code kills a hook at 60 s by default, so any budget at or above that is not a budget — it is the host killing the process with no report from us. 15 s sits well under it, so the stop is ours and comes with a line.
before/after_save, before/after_consolidate 120 s nohup'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. 0 disables; an unparseable value falls back to the shipped default rather than letting $(( )) decide under set -u (the #258 lesson).


Implementation note: the healthy case stays free

A kill -0 poll on a one-second tick charges every hook a second it did not spend, and after_post_tool dispatches on every tool call. So the hook is started in the background and waited on directly — a hook returning in 40 ms costs 40 ms — with a sibling subshell sleeping out the budget beside it, cancelled the moment wait returns. Its stdio is pointed at /dev/null so it cannot hold open the command substitution user-prompt-hook.sh wraps the dispatch in.

The empty-hooks.d/ case (the shipped distribution: a .gitkeep and nothing else) still costs nothing — not a process, not a file. Budget resolution is lazy on first executable hook, same discipline #230 applied to id -u.

Not timeout(1) — macOS does not ship it. That is the finding 50-git-restore.sh already 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_slower compares 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):

8 failed, 5 passed in 262.63s
FAILED ...::test_a_hook_that_never_returns_does_not_stall_the_prompt
       subprocess.TimeoutExpired: 'user-prompt-hook.sh' timed out after 30 seconds
FAILED ...::test_the_stop_reaches_the_file_the_doctor_reads
FAILED ...::test_a_timeout_is_reported_as_UNKNOWN_not_as_a_verdict_from_the_hook
FAILED ...::test_one_slow_hook_does_not_spend_the_next_hook_s_allowance
FAILED ...::test_a_hook_that_traps_gets_to_clean_up_before_KILL
FAILED ...::test_a_hook_that_ignores_TERM_is_still_stopped
FAILED ...::test_what_a_hung_hook_managed_to_say_is_still_delivered
FAILED ...::test_a_hook_s_detached_work_survives_the_kill

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:

13 passed in 55.58s

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 a hooks.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 — a hooks block plus a _comments entry.

Closes #286

…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>
@fdaviddpt
fdaviddpt merged commit 6d19ff7 into main Aug 1, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/286-dispatch-timeout branch August 1, 2026 17:28
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.

No dispatched hook has a timeout, so one that hangs stalls the agent indefinitely and logs nothing

1 participant