When a hook crashes or times out, Claude Code still carries out the action on most events. A guard hook with a mistyped path in settings.json then lets everything through. Since v2.1.295, the onFailure field handles that case. With "onFailure": "block", a failing hook blocks the action instead of letting it pass, which makes the hook fail closed. The field exists on command and http hooks and takes "continue" (the default) or "block".
If hooks are new to you, start with what hooks are in Claude Code.
Why does a crashing hook let the action through?
Claude Code reads the hook’s exit code. 0 means success, and 2 is a blocking error that stops the action on events that can be blocked. Anything else is a non-blocking error, and the action goes ahead.
A missing script lands in that bucket, and so does exit 1 without valid JSON, even though 1 is the usual Unix failure code. JSON on stdout that can’t be parsed is treated the same way. On PreToolUse, a hook that reaches its timeout doesn’t block either, and the tool call continues through the normal permission flow. The docs warn you not to count on a stalled hook to act as a gate.
All you see in the transcript is a <hook name> hook error notice. For a missing script run through the shell, it looks like this:
Failed with non-blocking status code: /bin/sh: /path/to/hook.sh: No such file or directory
How do I make a hook block the action when it fails?
Add "onFailure": "block" to the hook. This example from the docs, in .claude/settings.json, runs a project script before each Bash command and blocks the command if the script fails:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "node",
"args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/check-command.js"],
"onFailure": "block"
}
]
}
]
}
}
The hook uses exec form. command holds only the executable, and args holds the argument list. Claude Code spawns the program with no shell, so each args element is passed as one argument exactly as written, and ${CLAUDE_PROJECT_DIR} is substituted as a plain string. The docs recommend args whenever a hook references a path placeholder like this one, since nothing needs quoting.
What counts as a hook failure?
| Case | What it covers |
|---|
| Can’t start | The script or executable doesn’t exist |
| Exit code other than 0 or 2 | An exit 1, even if the hook printed JSON that allows the action, such as permissionDecision: "allow" |
| HTTP error | The connection fails, or the response status isn’t 2xx |
| Timeout | The hook reaches its timeout: 600 seconds by default for command and http, 30 on UserPromptSubmit |
| Invalid output | The JSON can’t be parsed or fails schema validation. For an HTTP hook, a 2xx body that is neither empty nor a JSON object counts too |
Plain-text stdout from a command hook isn’t a failure. To return a JSON decision, exit 0.
What does a failure block on each event?
With "block", a failure does what exit code 2 does on that event. PermissionRequest is the exception. A failure there denies the request, while exit code 2 isn’t honored on that event.
| Event | Effect of a failure with "block" |
|---|
PreToolUse | The tool call is blocked |
PermissionRequest | The permission request is denied |
UserPromptSubmit | The prompt is blocked and never reaches Claude |
Stop, SubagentStop, TaskCompleted, TeammateIdle | None, the field has no effect |
Hooks run with async or asyncRewake | None, the field has no effect |
On Stop and the other three events in that row, exit code 2 sends Claude back to work. The docs point out that Claude can’t repair a hook that won’t run, which is why the field does nothing there.
How do I test that the hook fails closed?
Use the config above without creating check-command.js, then ask Claude to run a command such as ls. Claude Code blocks the call, and the error includes failed; blocking because onFailure is "block" followed by node’s own message:
PreToolUse:Bash hook error: [node ${CLAUDE_PROJECT_DIR}/.claude/hooks/check-command.js]: failed; blocking because onFailure is "block"
Error: Cannot find module '/path/to/project/.claude/hooks/check-command.js'
After a timeout, the message says timed out instead of failed. Without onFailure, the same missing script is a non-blocking error and ls runs.
For the details, start claude --debug and read the log at ~/.claude/debug/<session-id>.txt. Since v2.1.296, every command hook on a tool event leaves a Hooks: line there that says how it ended, such as failed to start or timed out after <N>ms.
What should change in a script that warns with exit 1?
With "block", an exit 1 blocks the action just like a crash. If your script exits 1 for a mere warning, it now blocks those calls. The docs suggest ending with exit 0 where the script only warns. To still show the warning on PreToolUse, print it in the systemMessage field of a single JSON object:
echo '{"systemMessage": "This command touches a shared folder"}'
exit 0
Keep exit 2 for real denials. Exit code 2 is still a regular blocking error rather than a failure, so the stderr you write stays the reason Claude receives.
When should a hook keep the default?
Keep "block" for hooks that enforce a rule, like checking Bash commands, scanning for secrets or calling a validation service over HTTP. For a formatting, notification or logging hook, the "continue" default keeps a harmless crash from stopping Claude.
With an HTTP hook set to "block", an outage of the service blocks every call the hook watches. Keep its matcher narrow and give it a timeout that fits the service.
The field needs v2.1.295 or later, and claude -v prints your version. In an organization, managed settings can enforce that floor with requiredMinimumVersion, and Claude Code then refuses to start on an older version:
{
"requiredMinimumVersion": "2.1.295"
}
Module 5, Hooks and automation walks through a hook’s structure, matcher and timeout, then PreToolUse guards. Module 11, Security and compliance shows security and audit hooks to combine with permissions and managed settings.