Claude Code Hook Events Fired Twice When Running from Home Directory

Resolved 💬 6 comments Opened Jul 14, 2025 by coygeek Closed Jul 18, 2025

Claude Code Hook Events Fired Twice When Running from Home Directory

Bug Description

Claude Code fires duplicate Stop hook events when executed from the user's home directory (/Users/username), but works correctly when executed from other directories. This causes duplicate notifications, sounds, and any other hook-based integrations to trigger twice.

Environment

  • Platform: macOS (Darwin 24.6.0)
  • Claude Code Version: Latest (as of 2025-07-14)
  • Shell: bash/zsh
  • Home Directory: /Users/username

Steps to Reproduce

  1. Configure Claude Code hooks in ~/.claude/settings.json:
{
  "hooks": {
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "/path/to/your/hook-script.sh"
          }
        ]
      }
    ]
  }
}
  1. Open terminal in home directory (/Users/username)
  2. Run claude
  3. Send any message (e.g., "hello")
  4. Exit the session
  5. Observe hook execution
  1. Compare with other directory:
  • cd /Users/username/Desktop/test
  • Run claude
  • Send any message
  • Exit the session
  • Observe hook execution

Expected Behavior

Hook events should fire exactly once per event regardless of the working directory from which Claude Code is executed.

Actual Behavior

  • From home directory (/Users/username): Hook events fire twice
  • From other directories: Hook events fire once (correct behavior)

Evidence

Log Analysis

Hook script logs clearly show duplicate event processing when running from home directory:

From /Users/username (DUPLICATE EVENTS):

[2025-07-13 20:08:00] [INFO] === Claude Hook v18.0 started ===
[2025-07-13 20:08:00] [INFO] === Claude Hook v18.0 started ===
[2025-07-13 20:08:00] [INFO] Processing event: Stop
[2025-07-13 20:08:00] [INFO] Processing event: Stop

From /Users/username/Desktop/test (CORRECT BEHAVIOR):

[2025-07-13 20:04:50] [INFO] === Claude Hook v18.0 started ===
[2025-07-13 20:04:50] [INFO] Processing event: Stop

Notification Behavior

  • Home directory: 2 identical notifications displayed
  • Other directories: 1 notification displayed (expected)

Root Cause Analysis

Based on extensive debugging, Claude Code appears to launch two separate hook processes simultaneously when executed from the user's home directory, but only one process when executed from other directories.

This suggests a potential issue in Claude Code's hook registration or execution logic that specifically affects the home directory context, possibly related to:

  1. Configuration loading hierarchy - Different behavior when Claude Code runs from the same directory as the global configuration
  2. Process spawning logic - Potential race condition or double-execution in home directory context
  3. Session management - Different session handling when working directory matches configuration directory

Impact

  • User Experience: Annoying duplicate notifications
  • Automation: Scripts/integrations receive duplicate events
  • Resource Usage: Unnecessary duplicate process execution
  • Reliability: Inconsistent behavior between directories

Workaround

We implemented client-side deduplication using file locking in the hook script:

# Add deduplication logic with file locking to handle concurrent processes
acquire_lock() {
    local timeout=10
    local count=0
    while [[ $count -lt $timeout ]]; do
        if (set -C; echo $$ > "$LOCK_FILE") 2>/dev/null; then
            return 0  # Lock acquired
        fi
        sleep 0.1
        count=$((count + 1))
    done
    return 1  # Failed to acquire lock
}

is_duplicate_event() {
    local event_type="$1"
    local session_id="$2"
    local current_epoch=$(date '+%s')
    
    if ! acquire_lock; then
        return 1  # Allow event if we can't get lock
    fi
    
    # Check recent events and record if not duplicate
    # ... deduplication logic ...
    
    release_lock
    return $is_duplicate
}

Suggested Fix

The issue should be addressed in Claude Code's core hook execution logic to ensure:

  1. Single Process Execution: Only one hook process should be spawned per event
  2. Consistent Behavior: Same execution pattern regardless of working directory
  3. Session Management: Proper session tracking that doesn't create duplicates based on directory context

Additional Notes

  • This bug is reproducible and consistent
  • Only affects the home directory - all other directories work correctly
  • Affects all hook events (Stop, Notification, etc.) when run from home directory
  • No configuration differences between directories - same global ~/.claude/settings.json used

Priority

Medium-High - While workaround exists, this affects user experience and could impact automation reliability for users running Claude Code from their home directory.

---

Reporter: [Your GitHub username]
Date: 2025-07-14
Claude Code Session: Multiple reproductions confirmed

View original on GitHub ↗

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