Skip to content

Commit 5a1a004

Browse files
authored
fix(server): stop printing a traceback to stderr for malformed JSON-RPC bodies (a2aproject#1301)
# Description When a JSON-RPC request body is not valid JSON, `JsonRpcDispatcher.handle_requests` called `traceback.print_exc()` before returning the `-32700` error. Every malformed client request therefore wrote a full stack trace to the server's stderr, outside the application's logging configuration (no level, no handler, no way to silence it). The same error is already logged at `WARNING` through the module logger by `_generate_error_response`, like every other request error, so the print only duplicated it. - Remove the `traceback.print_exc()` call and the now-unused import. - Regression test: a malformed body still returns `-32700`, nothing is written to stderr, and the module logger records a `WARNING`. It fails on main. Checks: `scripts/lint.sh` clean, `uv run pytest` 2181 passed (plus `tests/integration/cross_version` 5 passed), coverage 93%. - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - [x] Ensure the tests and linter pass - [x] Appropriate docs were updated (if necessary)
1 parent dac7d9d commit 5a1a004

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎src/a2a/server/routes/jsonrpc_dispatcher.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import json
44
import logging
5-
import traceback
65

76
from collections.abc import AsyncGenerator
87
from typing import TYPE_CHECKING, Any
@@ -327,7 +326,6 @@ async def handle_requests(self, request: Request) -> Response: # noqa: PLR0911,
327326
handler_result = build_error_response(request_id, e)
328327
return self._create_response(call_context, handler_result)
329328
except json.decoder.JSONDecodeError as e:
330-
traceback.print_exc()
331329
return self._generate_error_response(
332330
None, JSONParseError(message=str(e))
333331
)

‎tests/server/routes/test_jsonrpc_dispatcher.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ def test_parse_diagnostic_survives_in_metadata(self, parse_error):
157157
assert 'parts' in parse_error['data'][0]['metadata']['parseError']
158158

159159

160+
class TestMalformedJsonBody:
161+
def test_reported_through_logging_not_stderr(self, client, capsys, caplog):
162+
response = client.post(
163+
'/',
164+
content=b'{not json',
165+
headers={'Content-Type': 'application/json'},
166+
)
167+
168+
assert response.json()['error']['code'] == -32700
169+
assert 'Traceback' not in capsys.readouterr().err
170+
assert any(
171+
record.name == jsonrpc_dispatcher.__name__
172+
and record.levelname == 'WARNING'
173+
for record in caplog.records
174+
)
175+
176+
160177
class TestJsonRpcDispatcherOptionalDependencies:
161178
@pytest.fixture(scope='class')
162179
def mock_app_params(self) -> dict:

0 commit comments

Comments
 (0)