Skip to content

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

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions