Skip to content

Commit 5de4410

Browse files
committed
Update coverage
1 parent a2deae1 commit 5de4410

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

tests/shared/test_proxy.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _create() -> StreamsFixtureReturn:
5959
for stream in streams_to_cleanup:
6060
try:
6161
await stream.aclose()
62-
except Exception:
62+
except Exception: # pragma: no cover
6363
pass # Already closed
6464

6565

@@ -318,12 +318,7 @@ async def test_proxy_handles_closed_resource_error(create_streams: CreateStreams
318318
client_streams, server_streams, (client_read_writer, _), (_, server_write_reader) = create_streams()
319319

320320
try:
321-
errors: list[Exception] = []
322-
323-
def error_handler(error: Exception) -> None:
324-
errors.append(error)
325-
326-
async with mcp_proxy(client_streams, server_streams, onerror=error_handler):
321+
async with mcp_proxy(client_streams, server_streams):
327322
# Close the read stream to trigger ClosedResourceError
328323
client_read, _ = client_streams
329324
await client_read.aclose()
@@ -332,14 +327,50 @@ def error_handler(error: Exception) -> None:
332327
await anyio.sleep(0.1)
333328

334329
# Proxy should handle this gracefully without crashing
335-
# The ClosedResourceError is caught and logged, but not passed to onerror
336-
# (it's expected during shutdown)
330+
# The ClosedResourceError is caught and logged internally
337331
finally:
338332
# Clean up test streams
339333
await client_read_writer.aclose()
340334
await server_write_reader.aclose()
341335

342336

337+
@pytest.mark.anyio
338+
async def test_proxy_handles_write_stream_closed_during_forward(
339+
create_streams: CreateStreamsFixture,
340+
) -> None:
341+
"""Test that proxy handles write stream closing during message forwarding."""
342+
(
343+
client_streams,
344+
server_streams,
345+
(client_read_writer, _),
346+
(server_read_writer, server_write_reader),
347+
) = create_streams()
348+
349+
try:
350+
_client_read, client_write = client_streams
351+
352+
async with mcp_proxy(client_streams, server_streams):
353+
# Close the client write stream (which receives messages from server)
354+
await client_write.aclose()
355+
356+
# Now send a message from server that would need to be forwarded to client
357+
# This will trigger ClosedResourceError in the forward loop when trying
358+
# to write to the closed client_write stream
359+
request = JSONRPCRequest(jsonrpc="2.0", id="test", method="test", params={})
360+
message = SessionMessage(JSONRPCMessage(request))
361+
await server_read_writer.send(message)
362+
363+
# Give it time to process
364+
await anyio.sleep(0.1)
365+
366+
# Proxy should handle this gracefully without crashing
367+
finally:
368+
# Clean up test streams
369+
await client_read_writer.aclose()
370+
await server_read_writer.aclose()
371+
await server_write_reader.aclose()
372+
373+
343374
@pytest.mark.anyio
344375
async def test_proxy_closes_other_stream_on_close(create_streams: CreateStreamsFixture) -> None:
345376
"""Test that when one stream closes, the other is also closed."""

0 commit comments

Comments
 (0)