FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: avoid stdio cleanup BrokenResourceError race by lavish0000 · Pull Request #2219 · modelcontextprotocol/python-sdk · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions src/mcp/client/stdio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def stdout_reader():

session_message = SessionMessage(message)
await read_stream_writer.send(session_message)
except anyio.ClosedResourceError: # pragma: lax no cover
except (anyio.BrokenResourceError, anyio.ClosedResourceError): # pragma: lax no cover
await anyio.lowlevel.checkpoint()

async def stdin_writer():
Expand All @@ -174,7 +174,7 @@ async def stdin_writer():
errors=server.encoding_error_handler,
)
)
except anyio.ClosedResourceError: # pragma: no cover
except (anyio.BrokenResourceError, anyio.ClosedResourceError): # pragma: no cover
await anyio.lowlevel.checkpoint()

async with anyio.create_task_group() as tg, process:
Expand Down
29 changes: 29 additions & 0 deletions tests/client/test_stdio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,35 @@ async def test_stdio_client_universal_cleanup():
)


@pytest.mark.anyio
async def test_stdio_client_cleanup_cancels_backpressured_stdout_reader():
"""Regression test for issue #1960.

Exiting the client without consuming the read stream leaves stdout_reader
blocked on a zero-buffer send. Cleanup must cancel the task before closing
its memory stream.
"""
script_content = textwrap.dedent(
"""
import sys
import time

sys.stdout.write('{"jsonrpc":"2.0","id":1,"result":{}}\\n')
sys.stdout.flush()
time.sleep(2.0)
"""
)

server_params = StdioServerParameters(
command=sys.executable,
args=["-c", script_content],
)

with anyio.fail_after(5.0):
async with stdio_client(server_params) as (_, _):
await anyio.sleep(0.2)


@pytest.mark.anyio
@pytest.mark.skipif(sys.platform == "win32", reason="Windows signal handling is different")
async def test_stdio_client_sigint_only_process(): # pragma: lax no cover
Expand Down

Back | FazBrowse Home | New Git URL