diff options
| author | Jesse Vincent <jesse@fsck.com> | 2026-10-22 21:16:09 +0000 |
|---|---|---|
| committer | Jesse Vincent <jesse@fsck.com> | 2026-10-22 21:16:09 +0000 |
| commit | 328685babd94e11910516025dee12e32a6d49de1 (patch) | |
| tree | f53eccaac178fd54d38f480d16913f35efe5f971 /hooks/run-hook.cmd | |
| parent | 0f9581f6c1525b51dc1600b14d24f3db1f601446 (diff) | |
fix(hooks): start no child processes in the SessionStart hook (#2081)fix/2081-session-start-forks
On Windows, Git Bash emulates fork(), so every command substitution and
pipe costs tens of milliseconds. The SessionStart hook runs on every
startup, /clear and compaction, and on dev it forked six times: the
$(cd ... && pwd) in run-hook.cmd and twice in session-start,
$(escape_for_json ...), and printf | cat.
Resolve paths with parameter expansion, read the skill with the read
builtin, have escape_for_json assign a variable, and absorb EPIPE with
trap '' PIPE plus printf 2>/dev/null || true instead of piping through
cat. The cat pipe also turned an early-closed stdout into exit 141 under
pipefail; the new emit_json exits 0 there.
Measured on Windows 11 (VM, Git Bash 5.2), 21 interleaved runs of
run-hook.cmd session-start: median 116 ms before, 46 ms after, against a
25 ms floor for one bash start (the wrapper starts two). Claude Code
2.1.308 logged the hook at 181-421 ms before and 137-146 ms after. Output
is byte-identical to dev across all five output shapes, two working
directories, normal and empty PATH, direct and through the wrapper (40/40
on Linux), and its sha256 matches on Windows.
Tests: the cat-stub case, which asserted the pipe, is replaced by a
closed-stdout case, and a new case counts process creations with strace
on Linux (5 in session-start and 6 through the wrapper on dev, 0 after).
Co-authored-by: willow <willowite@users.noreply.github.com>
Claude-Session: https://claude.ai/code/session_01SHANcr4A71nzuU9et4Fnba
Diffstat (limited to 'hooks/run-hook.cmd')
| -rwxr-xr-x | hooks/run-hook.cmd | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/hooks/run-hook.cmd b/hooks/run-hook.cmd index a2f7c48..3ef045d 100755 --- a/hooks/run-hook.cmd +++ b/hooks/run-hook.cmd @@ -53,9 +53,11 @@ CMDBLOCK # running) instead of a PATH lookup for bash, so this keeps working when # Claude Code spawns the SessionStart hook with a broken/empty PATH # (anthropics/claude-code#43127). +# No $(cd ... && pwd) subshell: a fork costs tens of milliseconds under Git +# Bash on Windows, and a relative directory is enough for the exec below. case "$0" in - */*|*\\*) SCRIPT_DIR="$(cd "${0%[/\\]*}" && pwd)" ;; - *) SCRIPT_DIR="$(pwd)" ;; + */*|*\\*) SCRIPT_DIR="${0%[/\\]*}" ;; + *) SCRIPT_DIR="." ;; esac SCRIPT_NAME="$1" shift |
