Skip to content

[Bug] Subagent Docker containers not auto-removed after completion, causing maxConcurrent slot exhaustion #46254

Closed as not planned

Description

@2670044605

Bug Description

Subagent Docker containers are not automatically cleaned up after task completion, causing them to accumulate until maxConcurrent limit is reached and blocking new subagent spawns.

Observed Behavior

  • Subagent containers continue running after the task is complete
  • docker ps shows multiple openclaw-sbx-agent-main-subagent-* containers with status Up X hours
  • Once maxConcurrent (default 8) slots are filled, new sessions_spawn calls hang or fail
  • Manual docker kill + rm is required to restore functionality

Expected Behavior

Subagent containers should be automatically removed after the task completes (regardless of success/failure).

Environment

  • OpenClaw version: 2026.3.13
  • OS: Linux 6.12.73+deb13-amd64
  • Config: subagents.maxConcurrent: 8 (default)

Evidence

$ docker ps --filter name=subagent --format table {{.Names}}\t{{.Status}}

NAMES                                                    STATUS
openclaw-sbx-agent-main-subagent-d3c961bf-898-a7ed2519   Up 3 hours
openclaw-sbx-agent-main-subagent-f3abcd8c-036-592be4bb   Up 3 hours
openclaw-sbx-agent-main-subagent-4b22e7a5-985-1cd1e3bd   Up 4 hours
openclaw-sbx-agent-main-subagent-e7c48412-d91-d0d8516c   Up 5 hours
openclaw-sbx-agent-main-subagent-97497c4e-422-f78092f0   Up 5 hours
openclaw-sbx-agent-main-subagent-7a40e27d-a39-ad2d9c95   Up 6 hours

Workaround

Manual cleanup:

docker ps --filter name=subagent --format {{.Names}} | xargs docker rm -f

Root Cause Analysis

Likely missing --rm flag in Docker spawn logic or container lifecycle management issue.

Additional Context

This issue causes subagent spawns to hang indefinitely when the concurrent limit is reached, severely impacting workflows that rely on background task delegation.

Activity

  1. 2670044605 commented on Mar 14, 2026

    @2670044605
    Author

    Update: After cleanup, testing if the bug reproduces. Will update with results.

  2. 2670044605 commented on Mar 14, 2026

    @2670044605
    Author

    Proposed Fix

    After investigation, the issue appears to be in the Docker container lifecycle management.

    Root Cause

    The Docker spawn logic is likely missing the --rm flag or container cleanup is not triggered properly when subagent tasks complete/fail.

    Suggested Fix

    Option 1: Add --rm flag to Docker run command:

    const dockerArgs = [
      'run',
      '--rm',  // Auto-remove container on exit
      '--name', containerName,
      // ... other args
    ];

    Option 2: Implement explicit cleanup in orchestration layer:

    async function cleanupSubagentContainer(containerId: string) {
      try {
        await docker.container.get(containerId).remove({ force: true });
      } catch (e) {
        // Container may already be gone
      }
    }
    
    // Register cleanup handlers
    process.on('exit', cleanup);
    process.on('SIGTERM', cleanup);

    User Workaround

    Add to crontab for automatic cleanup:

    0 * * * * docker ps --filter "name=openclaw-sbx-agent-main-subagent" --format "{{.Names}}" | xargs -r docker rm -f >/dev/null 2>&1
  3. steipete commented on Apr 25, 2026

    @steipete
    Contributor

    Closing this as not reproducible on current main after Codex review.

    Current main does not support the reported causal path. Docker sandbox containers are intentionally persistent per sandbox scope and pruned on idle/age, while subagent concurrency limits are enforced by the in-process subagent lane and active-run registry rather than by counting running Docker containers.

    What I checked:

    • Sandbox containers are persistent by scope: Docs define sandbox scope as controlling how many containers exist (agent, session, shared), and ensureSandboxContainer() reuses an existing named container instead of removing it after each run. (src/agents/sandbox/docker.ts:524, c13836804058)
    • Persistence is documented, not accidental: Sandbox CLI docs explicitly say existing runtimes keep running and are normally pruned only after inactivity; defaults shown are idleHours: 24 and maxAgeDays: 7. Public docs: docs/cli/sandbox.md. (docs/cli/sandbox.md:151, c13836804058)
    • Prune logic removes old runtimes by idle/age: maybePruneSandboxes() checks idleHours/maxAgeDays and removes registered runtimes only when those thresholds are exceeded. (src/agents/sandbox/prune.ts:24, c13836804058)
    • Subagent maxConcurrent is a queue lane limit, not a container-slot limit: Docs define agents.defaults.subagents.maxConcurrent as the dedicated in-process subagent queue lane concurrency, and the queue implementation stores only active task ids per lane. (src/process/command-queue.ts:235, c13836804058)
    • Active-child gating ignores completed children: sessions_spawn blocks only when countActiveRunsForSession() reaches maxChildrenPerAgent; that counter excludes ended children once descendants are done, with direct regression coverage. (src/agents/subagent-registry-queries.ts:157, c13836804058)

    Review notes: reviewed against c13836804058.

  4. locked as resolved and limited conversation to collaborators on Apr 27, 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

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions