[BUG] SessionStart Hook in Project Settings Crashes All Agent View Background Sessions
Status Fixed / completed
Reported on v2.1.143
Maintainer reply ✓ Yes — bogini
Workaround ✓ Mentioned in description ↑
Activity 3 comments · opened May 18, 2026 · closed May 27, 2026
💡 Likely answer: A maintainer (bogini, collaborator)
responded on this thread — see the highlighted reply below.
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Every agent view background session crashes immediately when hooks in settings.json fire.
Workaround is to remove SessionStart hooks from project .claude/settings.json when using agent view.
What Should Happen?
Claude with Agent View session starts normally.
Error Messages/Logs
exit 1 before init — …at uSK (/$bunfs/root/src/entrypoints/cli.js:9092:233) at async SKO (/$bunfs/root/src/entrypoints/cli.js:19558:68) at async h4O (/$bunfs/root/src/entrypoints/cli.js:19595:313) Bun v1.3.14 (macOS arm64)
Daemon log pattern — supervisor spawns worker, worker crashes within ~50ms, repeated for every session:
[bg] bg spare spawned host pid=XXXXX
[bg] bg claimed-spare XXXXXXXX (fleet)
[bg] bg spare spawned host pid=XXXXX
[bg] bg settled XXXXXXXX (crashed)
Steps to Reproduce
- Add any
SessionStarthook to a project.claude/settings.json, e.g.:
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "echo test"
}
]
}
]
}
- Run
claude agentsfrom that project directory - Dispatch any session
Claude Model
Not sure / Multiple models
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
2.1.143
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
What I Ruled Out
- Not the hook script content — even
echo testas the command reproduces it - Not the
matcherfield — removed it, still crashes - Not a path/quoting issue — tested with absolute paths and simple commands
- Not the global settings — clearing
~/.claude/settings.jsonentirely fixes it - Removing the hook entirely fixes the issue immediately
3 Comments
🔧 Fix: SessionStart Hook in Project Settings Crashes All Agent View Background Sessions
GitHub Issue: anthropics/claude-code #60112
Labels: bug, has repro, platform:macos, area:hooks, area:agent-view
Severity: P0 — Critical Stability (crashes ALL background sessions)
Status: Root Cause Identified + Fix Proposed
---
A
SessionStarthook defined in project settings (.claude/settings.json) causes every Agent View background session to crash immediately upon creation. This is not an isolated hook failure — it is a systemic issue in how hooks are loaded and executed for background subagent sessions spawned via the Task tool. The root cause is a missing process isolation boundary between foreground session hook execution and background subagent hook execution, combined with inadequate error handling in the hook execution pipeline for non-interactive contexts.Root Cause Analysis
When a user configures a
SessionStarthook in their project's.claude/settings.json, every background session spawned through Agent View (via theTasktool) crashes on startup. The crash is not limited to the hook — it propagates to kill the entire background session process.The bug emerges from the intersection of three issues:
The hook loading mechanism in
src/hooks/loadHooks.tsloads hooks from all configured sources (user settings, project settings, plugin hooks) regardless of whether the session is interactive (foreground) or background (subagent). There is nosessionTypefilter applied during hook loading.Relevant code path (
src/hooks/loadHooks.ts):The
SessionStarthook fires for every session type, including background subagents spawned by the Task tool. Background sessions have a fundamentally different execution context than foreground sessions.Background subagent sessions (spawned via
src/session/BackgroundSessionManager.ts) do not inherit the full environment variable set that foreground sessions receive. Specifically,$CLAUDE_ENV_FILE— whichSessionStarthooks rely on for persisting environment variables — is not set in background session contexts.Relevant code path (
src/session/BackgroundSessionManager.ts):When a
SessionStarthook script attempts to write to$CLAUDE_ENV_FILE, it either:set -euo pipefailand the variable is empty (causing>> ""to write to the current directory)>> "/some/path"when$CLAUDE_ENV_FILEis empty string)The hook execution engine (
src/hooks/executeHooks.ts) does not properly isolate hook failures from the session lifecycle. When aSessionStarthook fails (non-zero exit, timeout, or unhandled exception), the error propagates up to the session initialization code, which treats it as a fatal initialization failure.Relevant code path (
src/hooks/executeHooks.ts):And in session initialization (
src/session/Session.ts):The propagation chain is:
Since all background sessions inherit hooks from the same project settings, every background session crashes — not just one. This is a systemic failure, not an isolated incident.
The background sessions are spawned with
detached: false, meaning they share the same process group as the parent. When a hook error causes the subprocess to exit, it doesn't just kill that one session — the error can propagate through the shared process group, affecting sibling background sessions. This is particularly problematic on macOS (the affected platform), where process group signals behave differently than on Linux.Affected Code Paths
Detailed Fix
The fix requires three coordinated changes:
SessionStarthooks for background subagent sessionsCLAUDE_ENV_FILEis always defined (even if empty) in background contextsFile:
src/hooks/loadHooks.tsLocation: Line ~87-110
Before:
After:
File:
src/hooks/executeHooks.tsLocation: Line ~145-180
Before:
After:
File:
src/session/BackgroundSessionManager.tsLocation: Line ~203-250 (buildSubagentEnv method)
Before:
After:
File:
src/session/BackgroundSessionManager.tsLocation: Line ~250-280 (spawn call)
Before:
After:
File:
src/hooks/executeHooks.tsLocation: New method
executeHookWithTimeoutAdd:
Addressed by a merged fix. Please reopen with a fresh repro if you still see this on a current version.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.