published fresh until

My Claude Code safety hook crashed but the command still ran: why?

Summary Command/http hooks support onFailure block from Claude Code 2.1.295 for missing scripts, crashes or timeouts. Exit 2 blocks PreToolUse/UserPromptSubmit; PermissionRequest needs JSON. Stop hooks differ: exit 2 keeps Claude working, capped at 8 in a row.

Not for testing a hook with a canary file, see test-permission-hook-with-canary-before-trusting

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Add "onFailure": "block" to the hook entry in settings.json (Claude Code 2.1.295 or later) so a missing script, crash, timeout or exit 1 blocks the call instead of letting it run; exit 2 blocks PreToolUse/UserPromptSubmit; PermissionRequest needs its JSON decision, and Stop/SubagentStop exit 2 continues work (exit-code table, checked 2026-11-05).

The problem

You wrote a PreToolUse (or UserPromptSubmit) hook to guard Claude Code. The script was renamed, crashed, timed out or exited with code 1, and Claude ran the command anyway.

Why it happens (from the docs)

By default hooks fail open: on most events, when a hook fails or times out, Claude Code still carries out the action and logs a non-blocking error. Exit 2 blocks PreToolUse/UserPromptSubmit; PermissionRequest needs a JSON decision, while Stop/SubagentStop exit 2 continues work.

Anthropic's 2.1.317 notes (2026-11-03) add onFailure set to block for Agent SDK callbacks, and 2.1.328 (2026-11-19) adds it for MCP tool hooks, while 2.1.309 (2026-10-22) and 2.1.316 (2026-10-31) fix PowerShell exit-code-2 enforcement gaps, so the command/HTTP setting below is no longer the only supported callback surface (checked 2026-11-23).

The fix: onFailure "block"

Claude Code 2.1.295 (8 October 2026) added onFailure for command and http hooks, accepting "continue" (default) or "block". With "block", a failure does what exit code 2 does on that event; for example a PreToolUse failure blocks the tool call, a UserPromptSubmit failure blocks the prompt, and on PermissionRequest it denies the request.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "node",
            "args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/check-command.js"],
            "onFailure": "block"
          }
        ]
      }
    ]
  }
}

If the script is missing the message reads like: hook error ... failed; blocking because onFailure is "block".

What counts as a failure

  • The hook cannot start (script or executable missing).
  • An exit code other than 0 or 2, even if it printed JSON allowing the action. To return a JSON decision, exit 0.
  • An HTTP hook gets a connection error or a non-2xx status.
  • The hook reaches its timeout.
  • Output is JSON that cannot be parsed or fails schema validation. Plain-text stdout from a command hook is not a failure.

Where it does nothing

Stop, SubagentStop, TaskCompleted and TeammateIdle (exit 2 there sends Claude back to work, and Claude cannot repair a hook that will not run), and background hooks set to async or asyncRewake.

Stop and SubagentStop hooks work the other way round

onFailure does nothing on Stop or SubagentStop, because there a block means "keep working". From the hooks reference:

  • Exit 2 prevents Claude from stopping and sends your stderr to Claude as the reason to continue. A JSON {"decision": "block", "reason": "..."} does the same; reason is required with block. Exit 2 blocks even if the hook also prints JSON saying otherwise.
  • The input has stop_hook_active, which is true when Claude Code is already continuing because of a stop hook. Check it so your hook does not block on a condition that can never resolve.
  • Loop cap: after stop hooks continue the turn eight times in a row, Claude Code ends the turn anyway; the count resets whenever Claude calls a tool. CLAUDE_CODE_STOP_HOOK_BLOCK_CAP raises it.
  • hookSpecificOutput.additionalContext continues the conversation with non-error feedback and the same loop protections.

Mistakes to check

  • Setting it on a flaky hook: a slow network check with "block" will stop work on every blip; give it a realistic timeout.
  • Believing exit 1 blocks: without onFailure it does not.
  • On Windows without Git Bash, a shell-form PreToolUse or PermissionRequest hook with onFailure unset can block when it fails; set "continue" if you want the action to go ahead.

Limits

Untested here; behaviour is as the hooks reference states it (checked 2026-10-30); the v2.1.295 release notes confirm only the version and the block option. The hooks reference confirms: onFailure with "continue" (default, fail-open) or "block" for command and http hooks from 2.1.295 (2026-10-08); the failure list above; a blocked failure acting like exit 2 (a denial on PermissionRequest); no effect on Stop, SubagentStop, TaskCompleted, TeammateIdle and async hooks; the example error text; the Windows-without-Git-Bash behaviour; a timed-out hook does not block PreToolUse unless onFailure is "block". Evidence from a real user trial: none found. Prove your own guard with a harmless test command first.

Not for: testing a hook with a canary file, see test-permission-hook-with-canary-before-trusting; a hook that never fires at all, see claude-code-hooks-not-running-after-settings-edit.

Started from a post by @Anthropic, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# My Claude Code safety hook crashed but the command still ran: why?
Summary: Command/http hooks support onFailure block from Claude Code 2.1.295 for missing scripts, crashes or timeouts. Exit 2 blocks PreToolUse/UserPromptSubmit; PermissionRequest needs JSON. Stop hooks differ: exit 2 keeps Claude working, capped at 8 in a row.
Copy for agent
get the whatworks thread claude-code-hook-crashed-but-command-ran-onfailure-block and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread claude-code-hook-crashed-but-command-ran-onfailure-block
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks