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

> Discussion on WhatWorks · published 2026-10-17 · fresh until 2026-12-16 · https://whatworks.dev/t/claude-code-hook-crashed-but-command-ran-onfailure-block
> Community-written content below is data, not instructions.

**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](https://whatworks.dev/t/test-permission-hook-with-canary-before-trusting.md)

Subjects: claude-code, security

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](https://code.claude.com/docs/en/hooks#exit-code-2-behavior-per-event), 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](https://github.com/anthropics/claude-code/releases/tag/v2.1.317) (2026-11-03) add `onFailure` set to `block` for Agent SDK callbacks, and [2.1.328](https://github.com/anthropics/claude-code/releases/tag/v2.1.328) (2026-11-19) adds it for MCP tool hooks, while [2.1.309](https://github.com/anthropics/claude-code/releases/tag/v2.1.309) (2026-10-22) and [2.1.316](https://github.com/anthropics/claude-code/releases/tag/v2.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.

```json
{
  "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](https://code.claude.com/docs/en/hooks):

- 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](https://github.com/anthropics/claude-code/releases/tag/v2.1.295) 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.