EINVAL error when watching temporary settings file during atomic write

Resolved 💬 3 comments Opened Dec 30, 2025 by aadelb Closed Jan 3, 2026

Bug Description

Claude Code throws an unhandled promise rejection when the file watcher tries to watch a temporary settings file that gets deleted during an atomic write operation.

Error Message

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: EINVAL: invalid argument, watch '/home/aadel/.claude/settings.local.json.tmp.2878648.1767121739221'
    at watch (unknown)
    at new FSWatcher (node:fs:30:31)
    at watch (node:fs:300:23)
    at oMI (/$bunfs/root/claude:389:6265)

Steps to Reproduce

  1. Have Claude Code running with file watching enabled
  2. Modify settings (either manually or through Claude Code operations)
  3. The atomic write creates a temp file, then renames it
  4. File watcher tries to attach to the temp file before it's renamed/deleted
  5. Error is thrown

Expected Behavior

The file watcher should:

  • Only watch the final destination file, not temp files
  • OR gracefully handle EINVAL errors for temp files that no longer exist
  • OR use a debounce/delay before attaching watchers

Environment

  • Claude Code Version: 2.0.76
  • Platform: Linux (Ubuntu/Debian)
  • Node/Bun: Bun runtime

Impact

  • Error is non-fatal but causes noise in logs
  • Does not affect functionality
  • Transient - usually doesn't recur

Suggested Fix

// Option 1: Ignore temp files in watcher
if (filePath.includes('.tmp.')) return;

// Option 2: Catch EINVAL in watcher setup
try {
  fs.watch(filePath, callback);
} catch (e) {
  if (e.code === 'EINVAL') {
    // File was deleted before watcher attached, ignore
    return;
  }
  throw e;
}

---
Reported by Ahmed Adel Bakr Alderai

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗