Skip to content

[Bug] A kept-alive stdio transport keeps the first client's session and silently ignores a second client's timeout #6155

Closed

Description

@BlueX888

What happened?

A keep_alive=True stdio transport shared by two clients keeps the first client's session settings. StdioTransport.connect() decides whether to reuse the kept-alive session by comparing TransportOptions only (session_class / forward_incoming_headers / backend_mode); the per-client session kwargs — read_timeout_seconds from Client(timeout=...), plus sampling/roots/elicitation handlers, the message handler and client_info — are not compared. The second client therefore talks to a session built for the first client, so its own timeout is silently ignored.

The guard's comment says this is exactly what it is meant to prevent ("handing it to a client that wants different ones would silently give it the first client's behavior"), but session kwargs are not part of the comparison.

Expected: each client's timeout= applies, or the mismatch guard raises like it does for differing TransportOptions.

Example Code

import asyncio
import pathlib
import tempfile
import textwrap
import time

from fastmcp import Client
from fastmcp.client.transports import PythonStdioTransport

server = pathlib.Path(tempfile.mkdtemp()) / "sleep_server.py"
server.write_text(
    textwrap.dedent(
        """
        import asyncio
        from fastmcp import FastMCP

        mcp = FastMCP("sleepy")

        @mcp.tool
        async def sleep(seconds: float) -> str:
            await asyncio.sleep(seconds)
            return "done"

        if __name__ == "__main__":
            mcp.run()
        """
    )
)


async def run(first_timeout: float, second_timeout: float) -> None:
    transport = PythonStdioTransport(script_path=str(server))  # keep_alive defaults to True
    async with Client(transport, timeout=first_timeout) as first:
        print("first client tools:", [t.name for t in await first.list_tools()])
    async with Client(transport, timeout=second_timeout) as second:
        started = time.monotonic()
        try:
            result = await second.call_tool("sleep", {"seconds": 3})
            print(
                f"second client ({second_timeout}s timeout): {result.data!r} "
                f"after {time.monotonic() - started:.2f}s"
            )
        except Exception as exc:
            print(
                f"second client ({second_timeout}s timeout) FAILED after "
                f"{time.monotonic() - started:.2f}s: {type(exc).__name__}: {exc}"
            )


asyncio.run(run(1, 5))
asyncio.run(run(5, 1))

Output on d07877e:

first client tools: ['sleep']
second client (5s timeout) FAILED after 1.00s: MCPError: Request 'tools/call' timed out

first client tools: ['sleep']
second client (1s timeout): 'done' after 3.05s

The second line is the same bug in the other direction: a 1 s timeout is ignored and the call runs for 3 s.

Version Information

FastMCP 4.2.0a2.dev10+d07877e (main, 2026-10-29)
Python 3.12.2
Windows 11

Related but distinct: #4471 (a kept-alive transport cannot be reused across concurrently-alive event loops), #5754 / #5760 (config-entry timeout field was ignored, closed).

Activity

  1. added
    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.
    clientRelated to the FastMCP client SDK or client-side functionality.
    on Oct 29, 2026
  2. BlueX888 commented on Oct 29, 2026

    @BlueX888
    ContributorAuthor

    @HootRock opened #6156 for this, and I independently verified both directions on 914210b with the reproduction above (details in the PR). The only failing check is check-issue-link: it requires the PR author to be assigned to this issue, so a maintainer needs to assign @HootRock (or advise otherwise) before it can merge.

  3. added a commit that references this issue on Oct 29, 2026
    0a125c1
  4. added a commit that references this issue on Nov 9, 2026
    872e12f
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

    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