fix: handle EAGAIN error when reading stdin in hook scripts#189
Open
chelseachen007 wants to merge 1 commit intoopenai:mainfrom
Open
fix: handle EAGAIN error when reading stdin in hook scripts#189chelseachen007 wants to merge 1 commit intoopenai:mainfrom
chelseachen007 wants to merge 1 commit intoopenai:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EAGAIN("resource temporarily unavailable") crash inreadHookInput()when stdin (fd 0) is in non-blocking mode and no data is availablefs.readFileSync(0, "utf8")with try-catch in bothstop-review-gate-hook.mjsandsession-lifecycle-hook.mjs, treatingEAGAIN/EWOULDBLOCKthe 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:This causes the stop hook to crash instead of gracefully proceeding with no input.
Test plan
EAGAIN/EWOULDBLOCK, all other errors re-throw{}(same as before for empty string)🤖 Generated with Claude Code