Skip to content

Stateless Mode memory leak #756

Open
@algirdasci

Description

@algirdasci

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)]

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Status

To triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions