fix: close MCP client on session_shutdown so pi -p can exit - #1
Open
d0ublecl1ck wants to merge 1 commit into
Open
fix: close MCP client on session_shutdown so pi -p can exit#1d0ublecl1ck wants to merge 1 commit into
d0ublecl1ck wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-mcpchild 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_shutdownhandler — 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.Why this is safe
session_shutdownfires 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
pi -p "ping" --provider <p> --model <m>hangs indefinitely after answering (verified against multiple providers).