Skip to content

stdio_client logs "Process group termination failed ... Operation not permitted" on macOS when terminating a busy child #3929

Backlog

Description

@securityvoid

Summary

On macOS, closing stdio_client for a child that has to be signalled
(it isn't exiting on stdin EOF) often logs a misleading warning, even
though termination succeeds.

Environment

mcp 1.30.0, Python 3.13, macOS (Darwin 27.2.0, arm64). Not tested on
Linux.

Reproduction

  1. Open stdio_client to a FastMCP stdio server.
  2. Start a tool call that blocks the child, for example a synchronous
    time.sleep(600).
  3. Leave the stdio_client context.
  4. Repeat 10 times.

The script is below.

Expected

No warning when the child is terminated successfully.

Actual

In 4 to 10 of 10 iterations, depending on timing:

WARNING mcp.os.posix.utilities: Process group termination failed for PID 4121: [Errno 1] Operation not permitted, falling back to simple terminate

The fallback process.terminate() succeeds, so nothing breaks, but the
warning is noisy and misleading. It does not occur when the child exits
by itself on stdin EOF, only when SIGTERM is needed.

Likely cause

In mcp/os/posix/utilities.py, after os.killpg(pgid, SIGTERM) the
code polls os.killpg(pgid, 0). Once the child has died but has not
been reaped (a zombie that is the only member of its group), macOS
returns EPERM instead of ESRCH, so the poll raises PermissionError
and the code takes the fallback path.

Treating PermissionError from that liveness poll as "group is gone"
would avoid the warning.

repro_sdk.py (usage: python repro_sdk.py <checkout-with-.venv> 10 hang)
"""usage: repro_sdk.py <project_dir> [iterations] [hang]
Uses only the MCP SDK (mcp 1.30.0): open a stdio_client to a trivial server, leave the context, count
'Process group termination failed' warnings. With `hang` the child is blocked (sync sleep) when closed."""
import asyncio, logging, os, sys, tempfile
logging.basicConfig(level=logging.WARNING, format="%(levelname)s %(name)s: %(message)s")
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
proj = sys.argv[1]; n = int(sys.argv[2]) if len(sys.argv) > 2 else 20; hang = "hang" in sys.argv
tmp = tempfile.mkdtemp()
open(tmp + "/child.py", "w").write(
    "import time\nfrom mcp.server.fastmcp import FastMCP\nm = FastMCP('c')\n@m.tool()\ndef hang() -> str:\n    time.sleep(600)\n    return 'x'\n@m.tool()\ndef ok() -> str:\n    return 'ok'\nm.run('stdio')\n")


async def one():
    params = StdioServerParameters(command=proj + "/.venv/bin/python", args=[tmp + "/child.py"])
    async with stdio_client(params) as (r, w):
        async with ClientSession(r, w) as s:
            await s.initialize()
            if hang:
                asyncio.create_task(s.call_tool("hang", {}))
                await asyncio.sleep(0.5)
            else:
                await s.call_tool("ok", {})

for i in range(n):
    asyncio.run(one())
print("done", n, "iterations")

Activity

  1. added
    bug
    v1
    on Nov 25, 2026
  2. kamalbuilds commented on Nov 26, 2026

    @kamalbuilds

    Confirmed this is v1.x only. On main, #2773 rewrote terminate_posix_process_tree: the liveness probe _group_alive treats EPERM as ambiguous and keeps polling, and the leader is reaped via process.returncode inside the loop, so the zombie case from this report no longer falls through to the warning.

    On v1.x, src/mcp/os/posix/utilities.py still catches PermissionError from the os.killpg(pgid, 0) poll in the outer except (ProcessLookupError, PermissionError, OSError), which logs "Process group termination failed" and falls back to process.terminate(). That matches the observed 4 to 10 of 10 on macOS.

    I'd like to send a minimal v1.x backport: handle PermissionError in the poll the same way main does, plus a test that simulates EPERM from the probe and asserts no warning is logged. Could this be assigned to me?

  3. mdayan8 commented on Dec 3, 2026

    @mdayan8

    Tested this on Linux, since the report notes it was only tried on macOS.

    Setup: Ubuntu 22.04 (kernel 5.15), Python 3.10, mcp 1.30.0 from PyPI, using the report's repro approach (FastMCP stdio server whose tool blocks in time.sleep(600), 8 iterations).

    Result: zero warnings in 8/8 iterations. The EPERM path is macOS-specific. On Linux, asyncio's child watcher (pidfd on modern kernels) reaps the leader right after SIGTERM lands, so the os.killpg(pgid, 0) poll raises ProcessLookupError on the next iteration and the loop exits cleanly. The zombie-only-group window that returns EPERM on macOS doesn't occur here.

    One thing that may confuse other testers: each close still takes about 2.8s on Linux in this scenario. That is not this bug. It's the deliberate 2s graceful-shutdown wait after stdin closes (PROCESS_TERMINATION_TIMEOUT in mcp/client/stdio), plus interpreter startup. I measured the same ~2.8s against a main-branch install, where terminate_posix_process_tree itself returns in ~0.01s for this case.

    I also confirmed @kamalbuilds' read of main vs v1.x at source level: on main, _group_alive treats EPERM as ambiguous and keeps polling while the leader is reaped via process.returncode, so the misleading warning is gone there. On the v1.x branch the PermissionError from the liveness poll still propagates into the outer handler that logs "Process group termination failed" and takes the fallback.

    For v1.x, the reporter's minimal fix (treat PermissionError from the signal-0 poll as "group is gone") would stop the warning; a more conservative backport would mirror main and keep polling on EPERM while reaping the leader. Happy to open the v1.x PR if a maintainer wants either version.

    I used AI assistance while setting up and running these tests; the measurements and analysis above are mine and reproducible with the steps described.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions