Skip to content

Running out of memory when using a combination of monkey patched asyncio.sleep and MCP Client #3895

@gediminasz

Description

@gediminasz

What happened?

I am making an MCP tool/task which uses await asyncio.sleep when polling some external API. I wanted to add some unit tests using the FastMCP Client and noticed a huge memory usage spikes:

Image

The pytest process needs to be killed otherwise it runs out of memory and my computer freezes.

Example Code

import asyncio
from unittest.mock import patch

from fastmcp import Client, FastMCP

mcp = FastMCP()


@mcp.tool(task=True)
async def sleepy():
    await asyncio.sleep(10)
    return {"success": True}


async def test_sleepy():
    with patch.object(asyncio, "sleep"):
        async with Client(transport=mcp) as client:
            await client.call_tool("sleepy")

Version Information

FastMCP version:                                                                 3.2.3
MCP version:                                                                    1.27.0
Python version:                                                                 3.14.3
Platform:                                Linux-6.8.0-107-generic-x86_64-with-glibc2.39

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.clientRelated to the FastMCP client SDK or client-side functionality.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions