claude-code - 💡(How to fix) Fix PostToolUse hook: exit code 2 does not block or undo the preceding write [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46468Fetched 2026-04-11 06:19:30
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

When a PostToolUse hook returns exit code 2, it has no effect on the tool result — the file has already been written. This is surprising behavior that is not documented in the hooks reference.

Root Cause

When a PostToolUse hook returns exit code 2, it has no effect on the tool result — the file has already been written. This is surprising behavior that is not documented in the hooks reference.

Fix Action

Workaround

Use PreToolUse hooks with exit 2 to block writes. Use PostToolUse hooks for informational output only (plain echo, no exit 2).

RAW_BUFFERClick to expand / collapse

Summary

When a PostToolUse hook returns exit code 2, it has no effect on the tool result — the file has already been written. This is surprising behavior that is not documented in the hooks reference.

Steps to reproduce

  1. Wire a PostToolUse hook on Edit or Write events.
  2. Have the hook return exit 2 with a message.
  3. Observe that the edit is persisted on disk regardless.
  4. Observe that agents reading the hook output may believe their edit failed and retry, causing confusion.

Expected behavior (from docs)

The hooks documentation implies exit code 2 blocks tool execution. This is true for PreToolUse but NOT for PostToolUse — the tool has already completed.

Actual behavior

  • PostToolUse fires after the write completes. The file is already on disk.
  • exit 2 from PostToolUse does not undo the write.
  • Agents that inspect hook output may interpret it as a failure, leading to retry loops.

Proposed fix

Add a clear note in the hooks documentation for PostToolUse events:

Note: PostToolUse hooks fire after the tool has completed. Returning exit 2 will not undo the tool's action. Use PreToolUse with exit 2 to block operations before they run.

Workaround

Use PreToolUse hooks with exit 2 to block writes. Use PostToolUse hooks for informational output only (plain echo, no exit 2).

Environment

Claude Code (CLI), hooks configured in .claude/settings.json.

extent analysis

TL;DR

To achieve the desired behavior of blocking tool execution, use PreToolUse hooks with exit 2 instead of PostToolUse hooks.

Guidance

  • When using PostToolUse hooks, be aware that they fire after the tool has completed its action, and returning exit 2 will not undo the action.
  • To block writes, use PreToolUse hooks with exit 2, as this will prevent the tool from executing.
  • Use PostToolUse hooks for informational output only, such as plain echo statements, without returning exit 2.
  • Review the hooks documentation and configuration in .claude/settings.json to ensure correct usage.

Example

No code snippet is provided as it is not necessary for this specific issue.

Notes

The proposed fix suggests adding a note to the hooks documentation to clarify the behavior of PostToolUse events. However, as a workaround, using PreToolUse hooks with exit 2 can achieve the desired behavior.

Recommendation

Apply the workaround by using PreToolUse hooks with exit 2 to block writes, as this provides a clear and effective solution to the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING