summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Vincent <jesse@fsck.com>2026-10-22 21:16:09 +0000
committerJesse Vincent <jesse@fsck.com>2026-10-22 21:16:09 +0000
commit328685babd94e11910516025dee12e32a6d49de1 (patch)
treef53eccaac178fd54d38f480d16913f35efe5f971
parent0f9581f6c1525b51dc1600b14d24f3db1f601446 (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
-rwxr-xr-xhooks/run-hook.cmd6
-rwxr-xr-xhooks/session-start53
-rwxr-xr-xtests/hooks/test-session-start.sh70
3 files changed, 83 insertions, 46 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
diff --git a/hooks/session-start b/hooks/session-start
index 63f82a6..d88e50f 100755
--- a/hooks/session-start
+++ b/hooks/session-start
@@ -9,24 +9,32 @@ set -euo pipefail
# dirname because a SessionStart:startup hook can run with a broken/empty
# PATH (anthropics/claude-code#43127) before any PATH-repairing sibling hook
# in the same startup batch takes effect.
+#
+# No command substitution or pipe runs in this script: each one forks, and a
+# fork costs tens of milliseconds under Git Bash on Windows. The paths stay
+# relative to the current directory, which is all the file read below needs.
case "$0" in
- */*|*\\*) SCRIPT_DIR="$(cd "${0%[/\\]*}" && pwd)" ;;
- *) SCRIPT_DIR="$(pwd)" ;;
+ */*|*\\*) SCRIPT_DIR="${0%[/\\]*}" ;;
+ *) SCRIPT_DIR="." ;;
esac
-PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
+PLUGIN_ROOT="${SCRIPT_DIR}/.."
-# Read using-superpowers content via bash's builtin $(< file) instead of
-# cat, for the same PATH-independence reason.
+# Read using-superpowers content with the read builtin (no cat, for the same
+# PATH-independence reason, and no $(< file) subshell). read returns 1 at end
+# of file, hence || true; the loop drops trailing newlines as $(< file) did.
skill_md="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md"
+using_superpowers_content="Error reading using-superpowers skill"
if [ -r "$skill_md" ]; then
- using_superpowers_content="$(<"$skill_md")"
-else
- using_superpowers_content="Error reading using-superpowers skill"
+ IFS= read -r -d '' using_superpowers_content < "$skill_md" || true
+ while [[ $using_superpowers_content == *$'\n' ]]; do
+ using_superpowers_content="${using_superpowers_content%$'\n'}"
+ done
fi
# Escape string for JSON embedding using bash parameter substitution.
# Each ${s//old/new} is a single C-level pass - orders of magnitude
-# faster than the character-by-character loop this replaces.
+# faster than the character-by-character loop this replaces. The result
+# goes to a variable instead of stdout, so no $(...) subshell is needed.
escape_for_json() {
local s="$1"
s="${s//\\/\\\\}"
@@ -34,10 +42,10 @@ escape_for_json() {
s="${s//$'\n'/\\n}"
s="${s//$'\r'/\\r}"
s="${s//$'\t'/\\t}"
- printf '%s' "$s"
+ using_superpowers_escaped="$s"
}
-using_superpowers_escaped=$(escape_for_json "$using_superpowers_content")
+escape_for_json "$using_superpowers_content"
session_context="<EXTREMELY_IMPORTANT>\nYou have superpowers.\n\n**Below is the full content of your 'superpowers:using-superpowers' skill - your introduction to using skills. For all other skills, use the 'Skill' tool:**\n\n${using_superpowers_escaped}\n</EXTREMELY_IMPORTANT>"
# Output context injection as JSON.
@@ -51,21 +59,18 @@ session_context="<EXTREMELY_IMPORTANT>\nYou have superpowers.\n\n**Below is the
# Uses printf instead of heredoc to work around bash 5.3+ heredoc hang.
# See: https://github.com/obra/superpowers/issues/571
#
-# emit_json pipes printf through `cat` when cat is on PATH, to absorb EPIPE
-# on Windows. On Windows + Git Bash, the harness can close the hook's stdout
-# before printf finishes, and Git Bash's printf reports that EPIPE as
-# "printf: write error: Permission denied", which `set -euo pipefail` lets
-# escape as a startup diagnostic every session. Letting cat receive the EPIPE
-# keeps it out of the hook's error path (#1612). When PATH is broken and cat
-# can't be found (#2310), it falls back to printf alone so context still
-# gets injected.
+# emit_json absorbs EPIPE without a pipe through `cat`. On Windows + Git
+# Bash, the harness can close the hook's stdout before printf finishes, and
+# Git Bash's printf reports that EPIPE as "printf: write error: Permission
+# denied", which `set -euo pipefail` would let escape as a startup diagnostic
+# every session (#1612). SIGPIPE is ignored so other platforms see the same
+# EPIPE error instead of a signal; the error message is dropped and the
+# status ignored. No external command is involved, so a broken PATH (#2310)
+# changes nothing.
emit_json() {
+ trap '' PIPE
# shellcheck disable=SC2059 # every caller passes a literal format string
- if command -v cat >/dev/null 2>&1; then
- printf "$@" | cat
- else
- printf "$@"
- fi
+ printf "$@" 2>/dev/null || true
}
if [ -n "${CURSOR_PLUGIN_ROOT:-}" ]; then
diff --git a/tests/hooks/test-session-start.sh b/tests/hooks/test-session-start.sh
index 6ddf011..163686f 100755
--- a/tests/hooks/test-session-start.sh
+++ b/tests/hooks/test-session-start.sh
@@ -294,27 +294,57 @@ assert_command_output \
CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \
"$bash_bin" -c 'cd "$1" && exec "$2" "$3"' _ "$backslash_cwd" "$bash_bin" "$backslash_hook"
-# When cat is on PATH, the JSON must still go through it: the pipe absorbs
-# EPIPE on Windows + Git Bash (#1612). The stub records that it ran.
-cat_stub_dir="$TEST_ROOT/cat-stub/bin"
-cat_marker="$TEST_ROOT/cat-stub/used"
-mkdir -p "$cat_stub_dir"
-printf '#!%s\n: > "%s"\nexec /bin/cat "$@"\n' "$bash_bin" "$cat_marker" > "$cat_stub_dir/cat"
-chmod +x "$cat_stub_dir/cat"
-cat_stub_home="$(make_home cat-stub)"
-assert_command_output \
- "session-start with cat on PATH emits nested additionalContext" \
- "nested" \
- "" \
- "" \
- "$cat_stub_home" \
- PATH="$cat_stub_dir" \
- CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \
- "$bash_bin" "$HOOK_UNDER_TEST"
-if [[ -f "$cat_marker" ]]; then
- pass "session-start pipes its JSON through cat when cat is on PATH"
+# EPIPE on Windows + Git Bash (#1612): the harness can close the hook's stdout
+# before the JSON is written. The hook must still exit 0 with nothing on
+# stderr, directly and through run-hook.cmd. The reader (:) exits without
+# reading, so the write usually lands on a closed pipe.
+assert_closed_stdout() {
+ local description="$1"
+ shift
+ local err_file="$TEST_ROOT/closed-stdout.err"
+ local status
+ set +e
+ env -i PATH="${PATH:-}" HOME="$TEST_ROOT" CLAUDE_PLUGIN_ROOT="$REPO_ROOT" "$@" 2>"$err_file" | :
+ status="${PIPESTATUS[0]}"
+ set -e
+ if [[ "$status" -eq 0 && ! -s "$err_file" ]]; then
+ pass "$description"
+ else
+ fail "$description"
+ echo " exit $status, stderr:"
+ sed 's/^/ /' "$err_file"
+ fi
+}
+
+for _ in 1 2 3; do
+ assert_closed_stdout \
+ "session-start exits 0 silently when stdout is closed early" \
+ "$bash_bin" "$HOOK_UNDER_TEST"
+ assert_closed_stdout \
+ "run-hook.cmd exits 0 silently when stdout is closed early" \
+ "$bash_bin" "$WRAPPER_UNDER_TEST" session-start
+done
+
+# Each fork costs tens of milliseconds under Git Bash on Windows, and this hook
+# runs on every startup, /clear and compaction (#2081). Neither the hook nor
+# the wrapper may start a child process. Counted with Linux strace (Git for
+# Windows ships an unrelated strace.exe with other options).
+if [[ "$(uname -s)" == Linux ]] && command -v strace >/dev/null 2>&1; then
+ fork_log="$TEST_ROOT/forks.log"
+ for target in "$HOOK_UNDER_TEST" "$WRAPPER_UNDER_TEST session-start"; do
+ # shellcheck disable=SC2086 # split the wrapper's argument off its path
+ env -i PATH="${PATH:-}" HOME="$TEST_ROOT" CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \
+ strace -f -qq -e trace=fork,vfork,clone,clone3 -o "$fork_log" \
+ "$bash_bin" $target > /dev/null
+ forks="$(grep -c -E '(fork|clone3?)\(' "$fork_log" || true)"
+ if [[ "$forks" -eq 0 ]]; then
+ pass "${target##*/} starts no child processes"
+ else
+ fail "${target##*/} starts no child processes ($forks started)"
+ fi
+ done
else
- fail "session-start pipes its JSON through cat when cat is on PATH"
+ echo " [SKIP] child-process count: needs Linux strace"
fi
if [[ "$FAILURES" -gt 0 ]]; then