Skip to content

fix: shut down child sessions before disposal#170

Open
liu-qingyuan wants to merge 1 commit into
tintinweb:masterfrom
liu-qingyuan:fix/child-session-shutdown
Open

fix: shut down child sessions before disposal#170
liu-qingyuan wants to merge 1 commit into
tintinweb:masterfrom
liu-qingyuan:fix/child-session-shutdown

Conversation

@liu-qingyuan

Copy link
Copy Markdown

Summary

  • emit and await session_shutdown before disposing retained child AgentSession instances
  • apply the teardown contract to timed eviction, clearCompleted(), and manager shutdown
  • detach records before awaiting teardown so concurrent cleanup paths cannot shut down the same session twice
  • finish every child teardown with Promise.allSettled; log runner-level failures without blocking sibling or parent lifecycle cleanup

Problem

runAgent() creates a standalone session and binds extensions, but AgentSession.dispose() only invalidates the extension context. It does not emit session_shutdown. The parent manager therefore discarded retained child sessions without giving child extensions a chance to release timers, transports, or subprocesses.

The unref'd manager interval described in #126 is one symptom. A child extension with a referenced MCP transport can also keep a settled headless process alive indefinitely.

Design

AgentManager already owns child retention for resume and record eviction, so teardown stays private to that module. This mirrors the ordering used by Pi's AgentSessionRuntime.dispose() without migrating subagents to the larger runtime/session-replacement interface.

Completed sessions remain available for resume until the existing cleanup conditions remove their records. Production lifecycle handlers now await cleanup before proceeding.

Verification

  • npm run lint
  • npm run typecheck
  • npm run test — 752 passed, 5 skipped
  • npm run test:e2e — 47 passed, 5 skipped
  • npm run build
  • real Pi 0.81.1 probe with an auto-connected Xcode MCP child: parent reached agent_settled, exited naturally with code 0, and left no process after 3 seconds

Closes #126

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Child agent sessions never fire session_shutdown — per-spawn AgentManager/interval leak

1 participant