Skip to content

Stateless Mode memory leak #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
algirdasci opened this issue May 19, 2025 · 1 comment
Open

Stateless Mode memory leak #756

algirdasci opened this issue May 19, 2025 · 1 comment

Comments

@algirdasci
Copy link

Describe the bug
Stateless mode with Streamable-HTTP leaks memory. The StreamableHTTPSessionManager stateless server task group never exits and the _task list grows with each request leading to always increasing memory usage.

Expected behavior
Stateless server exits after handling the request freeing up the resources

Steps to reproduce

  1. Create minimal MCP server:
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("StatelessServer", stateless_http=True)


@mcp.tool()
def task_size() -> int:
    return len(mcp.session_manager._task_group._tasks)


if __name__ == "__main__":
    mcp.run(transport="streamable-http")
  1. Create minimal MCP client:
import asyncio

from fastmcp import Client
from fastmcp.client.transports import StreamableHttpTransport
from mcp.types import CallToolResult

async def execute_mcp_tool_call():
    transport = StreamableHttpTransport("http://localhost:8000/mcp/")

    async with Client(transport) as client:
        tools = await client.list_tools()
        print(tools)

        result = await client.call_tool("task_size")
        print(result)


asyncio.run(execute_mcp_tool_call())
  1. Do multiple requests
  2. Tool will return the size of pending tasks in session manager task group and it is always increasing:
[TextContent(type='text', text='23', annotations=None)]
[TextContent(type='text', text='27', annotations=None)]
[TextContent(type='text', text='31', annotations=None)]
@algirdasci
Copy link
Author

I've managed to workaround this by adding a timeout for stateless server, which works in my case, but maybe there would be better solution for that? Unfortunately, I'm not that good in python, so I'm not sure what the better approach could be.

26b9502

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant