fix(mcp): close legacy chat responses on HTTP errors (#3930)

main
KennySimpson 2026-10-08 01:25:18 +08:00 committed by GitHub
parent e8cc9c763e
commit 69a7fd9931
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 111 additions and 1 deletions

View File

@ -1,7 +1,9 @@
"""Regression tests for the SSE consumer shared by the two chat tools."""
import json
import threading
import unittest
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from unittest import mock
import requests
@ -12,6 +14,7 @@ import weknora_mcp_server as srv
class ChatSSETest(unittest.TestCase):
def setUp(self):
self.client = srv.WeKnoraClient("http://example.test/api/v1", "test-key")
self.addCleanup(self.client.session.close)
def consume(self, lines, *, agent=False):
response = mock.MagicMock()
@ -135,6 +138,113 @@ class ChatSSETest(unittest.TestCase):
result = self.consume(["data: " + payload, ""] * 3)
self.assertEqual(result["answer"], "hello" * 3)
def check_http_error_closes_in_both_chat_paths(self, status):
for agent in (False, True):
with self.subTest(status=status, agent=agent):
response = requests.Response()
response.status_code = status
response.url = "http://example.test/chat"
response.raw = mock.MagicMock()
with mock.patch.object(
self.client.session, "post", return_value=response
), mock.patch.object(
response, "close", wraps=response.close
) as close, mock.patch.object(
response, "iter_lines"
) as iter_lines:
with self.assertRaises(requests.HTTPError) as raised:
if agent:
self.client.agent_chat("session-1", "question", "agent-1")
else:
self.client.chat("session-1", "question")
self.assertIs(raised.exception.response, response)
close.assert_called_once_with()
response.raw.close.assert_called_once_with()
response.raw.release_conn.assert_called_once_with()
iter_lines.assert_not_called()
def test_bad_request_closes_streaming_response(self):
self.check_http_error_closes_in_both_chat_paths(400)
def test_service_unavailable_closes_streaming_response(self):
self.check_http_error_closes_in_both_chat_paths(503)
def test_real_http_error_closes_streaming_response_in_both_chat_paths(self):
class ErrorHandler(BaseHTTPRequestHandler):
def do_POST(self):
self.rfile.read(int(self.headers.get("Content-Length", "0")))
self.send_response(int(self.path.split("/")[1]))
body = b"backend error\n"
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, format, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), ErrorHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
self.addCleanup(thread.join, 5)
self.addCleanup(server.server_close)
self.addCleanup(server.shutdown)
port = server.server_address[1]
for status in (400, 503):
for agent in (False, True):
with self.subTest(status=status, agent=agent):
client = srv.WeKnoraClient(
f"http://127.0.0.1:{port}/{status}", "test-key"
)
client.session.trust_env = False
self.addCleanup(client.session.close)
with self.assertRaises(requests.HTTPError) as raised:
if agent:
client.agent_chat("session-1", "question", "agent-1")
else:
client.chat("session-1", "question")
response = raised.exception.response
self.addCleanup(response.close)
self.assertEqual(response.status_code, status)
self.assertTrue(response.raw.closed)
def test_post_connection_error_is_preserved_in_both_chat_paths(self):
for agent in (False, True):
with self.subTest(agent=agent):
error = requests.ConnectionError("connection failed")
with mock.patch.object(self.client.session, "post", side_effect=error):
with self.assertRaises(requests.ConnectionError) as raised:
if agent:
self.client.agent_chat("session-1", "question", "agent-1")
else:
self.client.chat("session-1", "question")
self.assertIs(raised.exception, error)
def test_stream_read_error_closes_response_in_both_chat_paths(self):
for agent in (False, True):
with self.subTest(agent=agent):
response = requests.Response()
response.status_code = 200
response.raw = mock.MagicMock()
error = requests.ConnectionError("stream read failed")
with mock.patch.object(
self.client.session, "post", return_value=response
), mock.patch.object(
response, "iter_lines", side_effect=error
), mock.patch.object(
response, "close", wraps=response.close
) as close:
with self.assertRaises(requests.ConnectionError) as raised:
if agent:
self.client.agent_chat("session-1", "question", "agent-1")
else:
self.client.chat("session-1", "question")
self.assertIs(raised.exception, error)
close.assert_called_once_with()
response.raw.close.assert_called_once_with()
response.raw.release_conn.assert_called_once_with()
if __name__ == "__main__":
unittest.main()

View File

@ -520,7 +520,6 @@ class WeKnoraClient:
url, json=body, stream=True,
timeout=(10, WEKNORA_CHAT_TIMEOUT),
)
response.raise_for_status()
answer_chunks: list = []
references: list = []
@ -531,6 +530,7 @@ class WeKnoraClient:
# Use context manager to ensure the connection is returned to the pool
# even when breaking early on a 'complete' event.
with response:
response.raise_for_status()
for raw_line in response.iter_lines():
if isinstance(raw_line, bytes):
raw_line = raw_line.decode("utf-8")