Skip to content

Terminate stdio MCP servers on shutdown to avoid process leaks#19753

Merged
etraut-openai merged 6 commits intomainfrom
etraut/mcp-lifecycle-management
Apr 28, 2026
Merged

Terminate stdio MCP servers on shutdown to avoid process leaks#19753
etraut-openai merged 6 commits intomainfrom
etraut/mcp-lifecycle-management

Conversation

@etraut-openai
Copy link
Copy Markdown
Collaborator

@etraut-openai etraut-openai commented Apr 27, 2026

Why

Several bug reports describe thread shutdown (including subagent threads) leaving stdio MCP server processes behind. These reports all point at the same lifecycle gap: Codex launches stdio MCP servers, but the session-level shutdown path does not explicitly close MCP clients or terminate the server process tree.

Fixes #12491
Fixes #12976
Fixes #18881
Fixes #19469

History

This is best understood as a regression/coverage gap in MCP session lifecycle management, not as stdio MCP cleanup being absent all along. #10710 added process-group cleanup for stdio MCP servers, but that cleanup only runs when the RmcpClient/transport is dropped. The older reports (#12491 and #12976) came after that cleanup existed, which suggests the remaining problem was that some higher-level shutdown paths kept the MCP manager alive or replaced it without explicitly draining clients. The newer reports (#18881 and #19469) exposed the same family around manager replacement and shutdown.

What changed

  • Added an explicit stdio MCP process handle in codex-rmcp-client so local MCP servers terminate their process group and executor-backed MCP servers call the executor process terminator.
  • Added RmcpClient::shutdown() and manager-level MCP shutdown draining so session shutdown, channel-close fallback, MCP refresh, and connector probing stop owned MCP clients.
  • Added regression coverage that starts a stdio MCP server, begins an in-flight blocking tool call, shuts down the client, and asserts the server process exits.

Verification

  • cargo test -p codex-rmcp-client

  • cargo test -p codex-mcp

  • just fix -p codex-rmcp-client

  • just fix -p codex-mcp

  • just fix -p codex-core

  • Manual before/after validation with a temporary repro script:

    • Pre-fix binary from HEAD^ (fed0a8f4fa): reproduced the leak with surviving MCP server and child PIDs, survivors=[77583, 77592], leaked=true.
    • Post-fix binary from this branch (67e318148b): verified both MCP processes were gone after interrupting codex exec, survivors=[], leaked=false.

@etraut-openai etraut-openai requested a review from a team as a code owner April 27, 2026 03:26
Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 67e318148b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/codex-mcp/src/mcp_connection_manager.rs Outdated
Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1133a8e4c2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/rmcp-client/src/stdio_server_launcher.rs Outdated
@etraut-openai etraut-openai changed the title Terminate stdio MCP servers on shutdown Terminate stdio MCP servers on shutdown to avoid process leaks Apr 27, 2026
Bojun-Vvibe added a commit to Bojun-Vvibe/oss-contributions that referenced this pull request Apr 27, 2026
- openai/codex#19773 permissions: require profiles in TUI thread state (merge-after-nits)
- openai/codex#19755 Add Responses stream lifecycle diagnostics (merge-after-nits)
- openai/codex#19753 Terminate stdio MCP servers on shutdown (merge-as-is)
- BerriAI/litellm#26581 fix(semantic-filter): run for anthropic_messages call type (merge-as-is)
@etraut-openai etraut-openai merged commit 4e0cf94 into main Apr 28, 2026
25 checks passed
@etraut-openai etraut-openai deleted the etraut/mcp-lifecycle-management branch April 28, 2026 16:30
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 28, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

2 participants