Stop hook fails with ENOENT: posix_spawn '/bin/sh' during session end
Description
When a Claude Code session ends, the Stop hook fails with:
Stop hook error: Failed with non-blocking status code: Error occurred while executing hook command: ENOENT: no such file or directory, posix_spawn '/bin/sh'
This occurs consistently when hooks are configured to run shell commands during the Stop lifecycle event.
Environment
- OS: macOS (Darwin 25.2.0)
- Claude Code: Latest version
- Plugin: claude-mem (hooks registered for Stop event)
Hook Configuration
The Stop hook runs commands via node (e.g., node "scripts/worker-service.cjs" hook claude-code summarize). The hooks are configured in hooks.json with the Stop event type.
Root Cause Hypothesis
The sandbox environment appears to tear down /bin/sh access before the Stop hook commands finish executing. Since posix_spawn requires /bin/sh to execute shell commands, this race condition causes the ENOENT error.
Key observations:
- The error specifically mentions
posix_spawn '/bin/sh'— the shell binary itself is inaccessible - This only happens during
Stophooks, not duringPostToolUseorUserPromptSubmithooks - The timing suggests the sandbox cleanup begins before Stop hooks complete
Expected Behavior
Stop hooks should complete execution before the sandbox environment is torn down. The /bin/sh binary should remain accessible for the duration of hook execution.
Steps to Reproduce
- Configure a Stop hook that runs a shell command (e.g., via claude-mem plugin or any custom hook)
- Start a Claude Code session
- End the session (type "exit", use
/clear, or let it complete naturally) - Observe the Stop hook error in the output
Suggested Fix
Ensure the sandbox teardown waits for all Stop hook processes to complete (or at minimum, keeps /bin/sh accessible) before cleaning up the environment.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗