Skip to content

fix: orphan sub-sessions not cleaned up when parent aborts #37314

Closed

Description

@zxkjack123

Description

When a parent session is aborted (e.g., due to timeout, user cancellation, or runtime error), its sub-agent sessions are not automatically cleaned up. The orphaned sub-sessions remain in tool-calls state, consuming resources indefinitely.

Steps to reproduce

  1. Start an agent session that dispatches sub-agents via task(subagent_type="...")
  2. Allow one or more sub-agents to get stuck (e.g., infinite explore loop)
  3. Abort the parent session
  4. Observe that child sub-agent sessions remain in tool-calls state indefinitely

Evidence

From a production opencode deployment:

  • 4 orphan sub-sessions currently stuck in tool-calls state with terminated parents:

    • ses_09e82cb4 (explore sub-agent) — parent task-executor completed days ago
    • ses_0c77d7ac (explore sub-agent) — parent fusion-deep-research completed
    • ses_0ddbf20a (critical-thinking sub-agent) — parent fusion-deep-research completed
    • ses_117c5c57 (critical-thinking sub-agent) — parent session completed
  • 5 mass-abort events where 3+ sub-agents were aborted together because their parent was aborted — but the cleanup was a batch abort, not a graceful shutdown:

    • ses_10049ce8: 4 children aborted together (3 pm-coordinator + 1 explore)
    • ses_0c781f98: 3 children aborted together (pm-coordinator + dify-kb-manager + communication-drafter)
    • ses_0d4228cd: 3 children aborted together
    • ses_0dab015c2: 3 explore children aborted together
    • ses_11846e3e: 3 children aborted together

Expected behavior

  1. Cascading abort: When a parent session is terminated, automatically abort all active child sub-sessions.
  2. Orphan detection: Periodic cleanup of sub-sessions whose parent session no longer exists or has been terminated.
  3. Graceful shutdown signal: When aborting, send a cancellation signal to sub-agents first, wait briefly for them to save state, then force-terminate.

Impact

  • Prevents resource leaks from orphaned sub-sessions
  • Avoids confusion when checking session status and finding "running" sessions with dead parents

Activity

  1. added
    needs:complianceThis means the issue will auto-close after 2 hours.
    and removed
    needs:complianceThis means the issue will auto-close after 2 hours.
    on Jul 16, 2026
  2. github-actions commented on Jul 16, 2026

    @github-actions
    Contributor

    Thanks for updating the issue! It now includes a clear description, reproduction steps, and supporting evidence. Everything looks good.

  3. goleary commented on Jul 22, 2026

    @goleary

    running into this too

  4. NamedIdentity commented on Aug 27, 2026

    @NamedIdentity

    #45482 addresses this. Cleanup currently works from one stale snapshot and skips an intermediate task that already finished, so work beneath it survives. The PR stops the session in view plus its provable running descendants.

  5. neriousy commented on Oct 22, 2026

    @neriousy
    Member

    Partly done: interrupting a parent now stops its foreground subagent too (since #34320, packages/core/src/tool/plugin/subagent.ts). Background subagents are detached and keep running after the parent is interrupted, and there's no orphan cleanup; #45482 was closed unmerged. Closing this tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions