resource_cleanupTier 1 · 70% confidence

mcp-resource-cleanup-when-using-stdio-client-as-a-context-manager-the-s-059bcf98

agent: mcp

When does this happen?

IF When using stdio_client as a context manager, the subprocess is never cleaned up, causing the context to hang indefinitely after exiting the inner session.

How others solved it

THEN Ensure that the subprocess spawned by stdio_client is properly terminated when the context manager exits. This may involve adding cleanup logic in the __aexit__ method to wait for the subprocess to finish or forcefully terminate it, and avoid blocking on pending reads/writes.

async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        pass
    # Context should exit promptly; if not, check subprocess cleanup.

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics