Skip to content

fix: close MCP client on session_shutdown so pi -p can exit - #1

Open
d0ublecl1ck wants to merge 1 commit into
porameht:mainfrom
d0ublecl1ck:fix/session-shutdown-close-mcp
Open

fix: close MCP client on session_shutdown so pi -p can exit#1
d0ublecl1ck wants to merge 1 commit into
porameht:mainfrom
d0ublecl1ck:fix/session-shutdown-close-mcp

Conversation

@d0ublecl1ck

Copy link
Copy Markdown

Problem

With this extension loaded, pi -p "ping" (non-interactive print mode) never exits after answering — the process hangs forever.

Root cause: the extension spawns the codebase-memory-mcp child process at load time (void register()StdioClientTransport) but never closes it. The child's stdio pipes keep the parent Node event loop alive, so pi cannot exit after the answer completes.

Fix

Register an idempotent session_shutdown handler — the documented hook for session-scoped resources (docs/extensions.md: "Register an idempotent session_shutdown handler to close any session-scoped resources you start") — that closes the MCP client, letting the event loop drain.

pi.on("session_shutdown", async () => {
  await client?.close().catch(() => undefined);
  client = undefined;
});

Why this is safe

session_shutdown fires only at session end (process exit or session switch), not per conversation turn — interactive sessions keep the MCP connection alive across turns, so normal behavior is unchanged.

Verification

  • Before: pi -p "ping" --provider <p> --model <m> hangs indefinitely after answering (verified against multiple providers).
  • After: same command exits cleanly in seconds with the extension fully loaded and functional.

The extension spawns the codebase-memory-mcp child process at load time
but never closes it. The child's stdio pipes keep the parent event loop
alive, so pi in non-interactive print mode (-p / --mode json) hangs
forever after answering instead of exiting.

Register an idempotent session_shutdown handler (the documented hook for
session-scoped resources) that closes the MCP client, letting the event
loop drain and the process exit normally.

Repro: pi -p "ping" with this extension loaded never exits.
After: exits cleanly; interactive sessions are unaffected because
session_shutdown only fires at session end/switch, not per turn.
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.

1 participant