fix: use async settings read with timeout in preSpawn path
Summary
readClaudeSettings()uses synchronousreadFileSyncto readsettings.json, called during the preSpawn startup task chain viagenerateHookSettingsFile()- On macOS, if
settings.jsonis a symlink to iCloud Drive (or any network/cloud filesystem), theopenat()syscall can block indefinitely when the process lacks TCC entitlements — a common situation for LaunchAgent-spawned daemon processes - This causes sessions to hang for minutes or never start
Changes
claudeSettings.ts: AddreadClaudeSettingsAsync()usingfs/promises.readFilewithAbortSignal.timeout(5000). The syncreadClaudeSettings()is preserved for backward compatibility.generateHookSettings.ts: ConvertgenerateHookSettingsFile()to async, using the newreadClaudeSettingsAsync().startHookServerTask.ts: UpdategenerateHookSettingsFiletype from(port: number) => stringto(port: number) => Promise<string>andawaitthe call. The task runner is already async.
Context
Discovered while debugging daemon-spawned sessions on macOS. The settings.json file was a symlink resolving through iCloud Drive (~/Library/Mobile Documents/...). macOS blocks openat() from LaunchAgent-spawned processes to iCloud Drive paths without TCC entitlements, causing the synchronous readFileSync to hang indefinitely and block the entire session startup.
Test plan
- [ ] Verify sessions start normally when
settings.jsonis a regular local file - [ ] Verify sessions start gracefully (with defaults) when
settings.jsonis on a slow/inaccessible filesystem (times out after 5s instead of hanging) - [ ] Verify
shouldIncludeCoAuthoredBy()still works (uses sync version, unaffected) - [ ] Verify existing tests pass with the async interface change
Summary by CodeRabbit
Release Notes
- Refactor
- Improved asynchronous operations in settings reading for better CLI responsiveness and reliability.
- Enhanced timeout handling during configuration loading to ensure more robust hook server initialization.
Walkthrough
Three files across Claude settings utilities are updated to convert synchronous file I/O operations to asynchronous patterns. A new async function readClaudeSettingsAsync is added with timeout handling, generateHookSettingsFile is made async, and the startup task is updated to properly await the result.
Changes
| Cohort / File(s) | Summary |
|---|---|
Async Claude Settings Utilities apps/cli/src/backends/claude/utils/claudeSettings.ts, apps/cli/src/backends/claude/utils/generateHookSettings.ts |
Added new readClaudeSettingsAsync() function with timeout handling and error logging. Updated generateHookSettingsFile() to async, now using the new async read variant instead of synchronous readClaudeSettings(). |
Startup Task Integration apps/cli/src/backends/claude/startup/tasks/startHookServerTask.ts |
Updated task parameters to expect generateHookSettingsFile returning Promise<string>. Call site updated to await the async generation result. |
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~8 minutes
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'fix: use async settings read with timeout in preSpawn path' accurately describes the main change: converting synchronous settings reads to asynchronous with a 5-second timeout in the startup initialization path. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing Touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
Greptile Summary
Adds async version of readClaudeSettings() with 5-second timeout to prevent indefinite blocking when reading from iCloud Drive symlinks in daemon contexts on macOS. The sync version is preserved for backward compatibility.
Critical Issues Found:
- Breaking change: Converting
generateHookSettingsFile()to async breaks the existing caller atrunClaude.ts:513which doesn't await the call - this will causehookSettingsPathto be a Promise instead of a string, leading to runtime errors - Test failures: All 4 tests in
generateHookSettings.test.tscall the now-async function withoutawait, causing test failures
Non-critical:
existsSync()in the async path could still block on slow filesystems, though less severely thanreadFileSync()
The PR correctly updates startHookServerTask.ts to await the async call, but misses the other caller and tests.
Confidence Score: 1/5
- This PR introduces critical breaking changes that will cause runtime errors
- Converting
generateHookSettingsFilefrom sync to async without updating all callers creates runtime bugs -runClaude.ts:513will assign a Promise tohookSettingsPathinstead of a string, and tests will fail - Pay close attention to
generateHookSettings.ts- the async conversion is incomplete and breaks existing code
Important Files Changed
| Filename | Overview |
|---|---|
| apps/cli/src/backends/claude/utils/claudeSettings.ts | Added async version of settings reader with timeout; sync version remains for backward compatibility |
| apps/cli/src/backends/claude/utils/generateHookSettings.ts | Converted to async function but missing await in existing caller at runClaude.ts:513 - will cause runtime error |
| apps/cli/src/backends/claude/startup/tasks/startHookServerTask.ts | Type signature correctly updated and properly awaits the async call |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[preSpawn startup] --> B[startHookServerTask]
B --> C[generateHookSettingsFile]
C --> D{readClaudeSettingsAsync}
D -->|timeout=5s| E[AbortSignal.timeout]
E --> F[fs.promises.readFile]
F -->|success| G[merge with hooks config]
F -->|timeout| H[return null, use defaults]
F -->|error| H
G --> I[write temporary settings file]
I --> J[return settings path]
K[runClaude.ts:513<br/>MISSING AWAIT] -.->|broken| C
style K fill:#ff6b6b,stroke:#c92a2a
style D fill:#51cf66,stroke:#2f9e44
style E fill:#51cf66,stroke:#2f9e44
Last reviewed commit: d53ed81