forked from modelcontextprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_leak_reproduction.py
More file actions
230 lines (181 loc) · 7.53 KB
/
resource_leak_reproduction.py
File metadata and controls
230 lines (181 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
Reproduction script for the resource leak I found in streamable_http.py
I noticed that when SSE streaming fails, the HTTP response doesn't get closed properly.
This happens in both _handle_sse_response and _handle_resumption_request methods.
The problem: if the async for loop throws an exception (like malformed JSON or network issues),
the response.aclose() call never happens because it's only in the success path.
Files affected:
- src/mcp/client/streamable_http.py (lines 336 and 251)
This can cause connection pool exhaustion over time in production.
"""
import asyncio
import sys
from pathlib import Path
# Add the mcp module to the path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from mcp.client.streamable_http import StreamableHTTPTransport
class MockResponse:
"""Simple mock to track if aclose() gets called"""
def __init__(self):
self.closed = False
self.close_count = 0
async def aclose(self):
self.closed = True
self.close_count += 1
print(f"Response closed (called {self.close_count} times)")
class MockEventSource:
"""Mock that throws an exception to simulate broken SSE"""
def __init__(self, response):
self.response = response
def __aiter__(self):
return self
async def __anext__(self):
# Simulate what happens when SSE parsing fails
raise Exception("SSE parsing failed - connection broken")
class MockTransport(StreamableHTTPTransport):
"""Mock that shows the same bug as the real code"""
def __init__(self):
super().__init__("http://test")
self.mock_response = MockResponse()
async def _handle_sse_response(self, response, ctx, is_initialization=False):
"""
This mimics the actual bug in the real code.
The problem: when the async for loop throws an exception,
response.aclose() never gets called because it's only in the success path.
"""
try:
event_source = MockEventSource(response)
async for sse in event_source:
# This never runs because the exception happens first
is_complete = False
if is_complete:
await response.aclose() # This is line 336 in the real code
break
except Exception as e:
print(f"Exception caught: {e}")
# Here's the bug - response.aclose() is never called!
raise
async def _handle_resumption_request(self, ctx):
"""
Same issue here - the aconnect_sse context manager should handle cleanup,
but if exceptions happen during SSE iteration, the response might not get closed.
"""
try:
# Mock the aconnect_sse context manager
class MockEventSourceWithResponse:
def __init__(self, response):
self.response = response
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
# Context manager exits but response might not be closed
pass
def __aiter__(self):
return self
async def __anext__(self):
raise Exception("Resumption SSE parsing failed")
async with MockEventSourceWithResponse(self.mock_response) as event_source:
async for sse in event_source:
# This never runs because the exception happens first
is_complete = False
if is_complete:
await event_source.response.aclose() # This is line 251 in the real code
break
except Exception as e:
print(f"Exception caught: {e}")
# Same bug here - response.aclose() is never called!
raise
async def test_resource_leak():
"""Test the resource leak I found"""
print("Testing resource leak in streamable_http.py")
print("=" * 50)
transport = MockTransport()
# Create mock context
class MockContext:
def __init__(self):
self.read_stream_writer = None
self.metadata = None
ctx = MockContext()
print("\nTesting _handle_sse_response method:")
print("-" * 35)
try:
await transport._handle_sse_response(transport.mock_response, ctx)
except Exception as e:
print(f"Caught expected exception: {e}")
# Check if response was closed
if transport.mock_response.closed:
print("No resource leak - response was closed properly")
return True
else:
print("RESOURCE LEAK DETECTED!")
print(f" Response closed: {transport.mock_response.closed}")
print(f" Close count: {transport.mock_response.close_count}")
print(" Expected: response.aclose() to be called in finally block")
return False
async def test_resumption_resource_leak():
"""Test the resource leak in _handle_resumption_request"""
print("\nTesting _handle_resumption_request method:")
print("-" * 40)
transport = MockTransport()
# Create mock context with resumption token
class MockResumptionContext:
def __init__(self):
self.read_stream_writer = None
self.metadata = type("obj", (object,), {"resumption_token": "test-token"})()
self.session_message = type(
"obj",
(object,),
{"message": type("obj", (object,), {"root": type("obj", (object,), {"id": "test-id"})()})()},
)()
ctx_resumption = MockResumptionContext()
try:
await transport._handle_resumption_request(ctx_resumption)
except Exception as e:
print(f"Caught expected exception: {e}")
# Check if response was closed
if transport.mock_response.closed:
print("No resource leak - response was closed properly")
return True
else:
print("RESOURCE LEAK DETECTED!")
print(f" Response closed: {transport.mock_response.closed}")
print(f" Close count: {transport.mock_response.close_count}")
print(" Expected: response.aclose() to be called in finally block")
return False
async def main():
"""Run the tests to show the resource leak"""
print("Resource Leak Test")
print("This shows the issue I found where HTTP responses don't get closed")
print("when SSE streaming fails in the MCP Python SDK.")
print()
# Test both methods
sse_leak = await test_resource_leak()
resumption_leak = await test_resumption_resource_leak()
print("\n" + "=" * 50)
print("SUMMARY:")
print("=" * 50)
if sse_leak and resumption_leak:
print("All tests passed - no resource leaks detected")
return 0
else:
print("Resource leaks confirmed in the following methods:")
if not sse_leak:
print(" - _handle_sse_response (line 336)")
if not resumption_leak:
print(" - _handle_resumption_request (line 251)")
print()
print("FIX NEEDED:")
print(" Add finally blocks to ensure response.aclose() is always called:")
print(" ```python")
print(" try:")
print(" # ... existing code ...")
print(" except Exception as e:")
print(" # ... existing exception handling ...")
print(" finally:")
print(" await response.aclose()")
print(" ```")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)