Skip to content

session-start hook hangs on bash 5.3+ due to heredoc bug with large variable expansion #571

Closed

Description

@pds

Bug

The session-start hook hangs indefinitely on systems where /usr/bin/env bash resolves to bash 5.3.9 (e.g., installed via Homebrew on macOS). Claude Code never finishes starting and the hook process must be killed manually from another terminal.

The system bash (/bin/bash v3.2 on macOS) works fine.

Root Cause

Bash 5.3 has a bug where heredoc variable expansion hangs when the expanded content exceeds ~512 bytes. The session-start script uses a cat <<EOF heredoc that expands ${session_context} — a ~4,500 byte string containing the escaped SKILL.md content. This triggers the bash 5.3 hang.

Lines 41-49 of hooks/session-start:

cat <<EOF
{
  "additional_context": "${session_context}",
  "hookSpecificOutput": {
    "hookEventName": "SessionStart",
    "additionalContext": "${session_context}"
  }
}
EOF

Minimal Reproducer

# Hangs in bash 5.3, works in bash 3.2
/opt/homebrew/bin/bash -c '
s=$(printf "%0.s=" {1..520})
cat <<EOF
${s}
EOF
'

Anything ≤ ~512 bytes works; anything larger hangs.

Suggested Fix

Replace the cat <<EOF heredoc with printf, which is not affected:

printf '{\n  "additional_context": "%s",\n  "hookSpecificOutput": {\n    "hookEventName": "SessionStart",\n    "additionalContext": "%s"\n  }\n}\n' "$session_context" "$session_context"

Environment

  • macOS 15 (Darwin 25.3.0, arm64)
  • Homebrew bash: GNU bash 5.3.9(1)-release (aarch64-apple-darwin25.1.0)
  • System bash: GNU bash 3.2.57(1)-release (arm64-apple-darwin25)
  • Superpowers plugin version: 4.3.1

Activity

  1. added
    bug
    hooks
    on Mar 4, 2026
  2. lukelabonte commented on Mar 12, 2026

    @lukelabonte

    Additional Context: Claude Code v2.1.72 Trigger + Secondary Bug

    I hit this same deadlock and traced it to the root cause. Adding some details that may be useful for the fix.

    Why This Started — Claude Code v2.1.72

    Claude Code v2.1.72 (released March 10, 2026) changed bash command parsing to use a native module:

    "Improved bash command parsing by switching to a native module — faster initialization and no memory leak"

    This was the fix for anthropics/claude-code#3991 (the original request to respect Homebrew bash). Hook commands are now spawned using PATH-resolved bash, so #!/usr/bin/env bash resolves to Homebrew bash 5.3.9 on machines that have it. Previously hooks used /bin/sh or system /bin/bash, which uses temp files for heredocs (no size limit).

    The pipe buffer deadlock mechanism is exactly as described in this issue — Homebrew bash 5.3.9 uses pipes for heredoc stdin redirection, and macOS pipe buffers are only 512 bytes. The session-start hook's cat <<EOF heredoc contains the escaped SKILL.md content (~5.2KB), well over the threshold.

    Secondary Bug: Unquoted Heredoc + Backtick Command Substitution

    The hook uses an unquoted heredoc (cat <<EOF instead of cat <<'EOF'), which means bash performs variable expansion and backtick command substitution on the heredoc content. The SKILL.md contains markdown backticks (inline code, code fences, a ```dot block with Graphviz DOT language).

    On machines with Graphviz installed (dot on PATH), the backtick content would trigger command execution — dot reads from stdin and hangs waiting for input. This is currently masked by the pipe buffer deadlock (which occurs first, before bash processes heredoc content), but it would surface if the pipe deadlock were fixed independently (e.g., by forcing system bash) while keeping the unquoted heredoc.

    The escape_for_json() function escapes \, ", newlines, carriage returns, and tabs — but does not escape backticks.

    Recommended Fix

    Replacing cat <<EOF ... EOF with printf '%s' "$session_context" eliminates both bugs:

    • No heredoc → no pipe buffer deadlock
    • No unquoted heredoc → no backtick command substitution

    I've been using this locally as a workaround and it works with both system and Homebrew bash.

    Cross-References

  3. added 2 commits that reference this issue on Mar 16, 2026
    537ec64
    689f27c
  4. added 2 commits that reference this issue on Mar 18, 2026
    d5e3bf9
    18cca3c
  5. added 2 commits that reference this issue on Apr 6, 2026
    b32164a
    baf39b5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions