Skip to content

Session token expires within minutes when multiple subagents are spawned concurrently #37324

Closed as not planned

Description

@nrep

Description

When spawning multiple subagents concurrently using the Agent tool with isolation: \"worktree\", some agents fail immediately with "Not logged in · Please run /login" even though the parent session was authenticated moments earlier.

Steps to Reproduce

  1. Start a Claude Code session (authenticated)
  2. Use the Agent tool to spawn 4+ subagents simultaneously with isolation: \"worktree\" and run_in_background: true
  3. Some agents (typically ~50% when spawning 4 at once) fail within seconds with "Not logged in"

Expected Behavior

All subagents should inherit/read the valid session credentials from ~/.claude/ without requiring re-authentication.

Actual Behavior

Some agents fail with "Not logged in · Please run /login" within seconds of being spawned. Running /login in the parent session fixes it temporarily, but the issue recurs on the next batch of concurrent spawns — sometimes within 5 minutes of the previous login.

Environment

  • Platform: macOS (darwin 25.2.0)
  • Shell: zsh
  • Concurrent agents spawned: 4 at a time
  • Parent session: also running inside a worktree

Hypothesis

Likely a race condition where concurrent subagent processes simultaneously read/write the ~/.claude/ credentials file, causing some to see an invalid/partial auth state.

Impact

Requires frequent manual /login interruptions during parallel agent workflows, which significantly disrupts autonomous multi-agent development pipelines.

Activity

  1. added
    bug
    platform:macos
    has repro
    on Mar 22, 2026
  2. github-actions commented on Mar 22, 2026

    @github-actions

    Found 3 possible duplicate issues:

    1. [BUG] Spawning subagents with isolation: "worktree" immediately fails auth #37258
    2. Background agents completing in parallel trigger 'Not logged in' and corrupt session credentials #37203
    3. Frequent re-authentication required with multiple concurrent Claude Code sessions (OAuth refresh token race condition) #24317

    This issue will be automatically closed as a duplicate in 3 days.

    • If your issue is a duplicate, please close it and 👍 the existing issue instead
    • To prevent auto-closure, add a comment or 👎 this comment

    🤖 Generated with Claude Code

  3. yurukusa commented on Mar 22, 2026

    @yurukusa

    This is likely a race condition in the auth token refresh mechanism. When multiple subagents read the credential file simultaneously, one reads while another is mid-write during token refresh, getting stale/empty credentials.
    Workaround: stagger the agent spawns with a small delay between each.
    In your CLAUDE.md:

    When spawning multiple agents in parallel, add a 2-second delay between each spawn.
    Do not spawn more than 3 agents simultaneously.
    

    Or enforce it with a PreToolUse hook that rate-limits Agent spawns:

    INPUT=$(cat)
    TOOL=$(echo "$INPUT" | jq -r '.tool_name // empty' 2>/dev/null)
    [[ "$TOOL" != "Agent" ]] && exit 0
    LOCK="/tmp/cc-agent-spawn-lock"
    NOW=$(date +%s)
    if [ -f "$LOCK" ]; then
        LAST=$(cat "$LOCK")
        DIFF=$((NOW - LAST))
        if (( DIFF < 3 )); then
            WAIT=$((3 - DIFF))
            echo "Rate limiting agent spawn (waiting ${WAIT}s)..." >&2
            sleep "$WAIT"
        fi
    fi
    echo "$NOW" > "$LOCK"
    exit 0

    This adds a minimum 3-second gap between agent spawns, reducing the chance of concurrent credential reads.

  4. nethalo commented on Mar 22, 2026

    @nethalo

    As a workaround, you can also just run /exit and then "claude --resume " and avoid the /login flow

  5. junaidtitan commented on Mar 22, 2026

    @junaidtitan

    We built and open-sourced cozempic — it continuously checkpoints agent team state so even if a session dies mid-flight, subagent results and task status are recoverable. The guard daemon also prunes bloat to keep sessions healthy under multi-agent load. Would love feedback.

  6. LARIkoz commented on Mar 23, 2026

    @LARIkoz

    Community workaround available: claude-batch — tmux-isolated parallel claude -p that prevents auth kickout. Drop-in replacement. See also RFC: #37678

  7. added
    stale
    on Apr 6, 2026
  8. github-actions commented on Apr 21, 2026

    @github-actions

    Closing for now — inactive for too long. Please open a new issue if this is still relevant.

  9. github-actions commented on May 2, 2026

    @github-actions

    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.

  10. locked as resolved and limited conversation to collaborators on May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:agentsarea:authbughas reproplatform:macosstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions