@@ -28,8 +28,7 @@ async def test_notification_validation_error(tmp_path: Path):
2828
2929 server = Server (name = "test" )
3030 request_count = 0
31- slow_request_started = anyio .Event ()
32- slow_request_complete = anyio .Event ()
31+ slow_request_lock = anyio .Event ()
3332
3433 @server .list_tools ()
3534 async def list_tools () -> list [types .Tool ]:
@@ -52,16 +51,9 @@ async def slow_tool(name: str, arg) -> Sequence[ContentBlock]:
5251 request_count += 1
5352
5453 if name == "slow" :
55- # Signal that slow request has started
56- slow_request_started .set ()
57- # Long enough to ensure timeout
58- await anyio .sleep (0.2 )
59- # Signal completion
60- slow_request_complete .set ()
54+ await slow_request_lock .wait () # it should timeout here
6155 return [TextContent (type = "text" , text = f"slow { request_count } " )]
6256 elif name == "fast" :
63- # Fast enough to complete before timeout
64- await anyio .sleep (0.01 )
6557 return [TextContent (type = "text" , text = f"fast { request_count } " )]
6658 return [TextContent (type = "text" , text = f"unknown { request_count } " )]
6759
@@ -90,16 +82,15 @@ async def client(read_stream, write_stream, scope):
9082 # First call should work (fast operation)
9183 result = await session .call_tool ("fast" )
9284 assert result .content == [TextContent (type = "text" , text = "fast 1" )]
93- assert not slow_request_complete .is_set ()
85+ assert not slow_request_lock .is_set ()
9486
9587 # Second call should timeout (slow operation)
9688 with pytest .raises (McpError ) as exc_info :
9789 await session .call_tool ("slow" )
9890 assert "Timed out while waiting" in str (exc_info .value )
9991
100- # Wait for slow request to complete in the background
101- with anyio .fail_after (1 ): # Timeout after 1 second
102- await slow_request_complete .wait ()
92+ # release the slow request not to have hanging process
93+ slow_request_lock .set ()
10394
10495 # Third call should work (fast operation),
10596 # proving server is still responsive
0 commit comments