Skip to content

fix: handle EAGAIN error when reading stdin in hook scripts#189

Open
chelseachen007 wants to merge 1 commit intoopenai:mainfrom
chelseachen007:fix/eagain-stdin-read
Open

fix: handle EAGAIN error when reading stdin in hook scripts#189
chelseachen007 wants to merge 1 commit intoopenai:mainfrom
chelseachen007:fix/eagain-stdin-read

Conversation

@chelseachen007
Copy link
Copy Markdown

Summary

  • Fix EAGAIN ("resource temporarily unavailable") crash in readHookInput() when stdin (fd 0) is in non-blocking mode and no data is available
  • Wraps fs.readFileSync(0, "utf8") with try-catch in both stop-review-gate-hook.mjs and session-lifecycle-hook.mjs, treating EAGAIN/EWOULDBLOCK the same as empty input (returns {})

Problem

When Claude Code runs hook scripts, stdin may be set to non-blocking mode (O_NONBLOCK). If no data is available on fd 0, fs.readFileSync(0, "utf8") throws:

Error: EAGAIN: resource temporarily unavailable, read
    at Object.readFileSync (node:fs:442:20)
    at readHookInput (stop-review-gate-hook.mjs:22:18)

This causes the stop hook to crash instead of gracefully proceeding with no input.

Test plan

  • Verified the fix locally — hooks no longer crash on session stop
  • Confirmed that normal stdin piping (when data IS available) still works — the try-catch only intercepts EAGAIN/EWOULDBLOCK, all other errors re-throw
  • Behavior is backward-compatible: when stdin has data, parsing works identically; when stdin is empty or unavailable, returns {} (same as before for empty string)

🤖 Generated with Claude Code

When Claude Code runs hook scripts, stdin (fd 0) may be set to
non-blocking mode (O_NONBLOCK). If no data is available,
`fs.readFileSync(0, "utf8")` throws EAGAIN ("resource temporarily
unavailable"), causing the hook to crash instead of gracefully
handling the empty input.

This wraps the readFileSync(0) calls in readHookInput() with a
try-catch that treats EAGAIN/EWOULDBLOCK the same as empty input
(returns {}), matching the existing behavior for empty stdin.

Affected scripts:
- stop-review-gate-hook.mjs
- session-lifecycle-hook.mjs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@chelseachen007 chelseachen007 requested a review from a team April 9, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants