Repository navigation
Commit 9e768fd
fix: answer the peer when a response payload cannot be encoded
A response payload is attributed to a request id before it is encoded, and
json.dumps runs inside transport.send. So any payload the serialiser refuses
makes the TypeError escape the response task, no reply is ever written, and
the peer waits on that id forever.
Two routes reach it today with no hostile input at all:
- a handler returning a raw value that is not JSON-native, e.g. {"x": {1, 2}};
a BaseModel result is safe because model_dump(mode="json") coerces, but a
raw return value was assigned unchecked;
- a handler raising RequestError(..., data=...), whose data is typed Any and
passed straight through to the error object.
Gate the finished payload once, at the end of _execute_request, with the same
json.dumps the transports call. Pydantic's to_jsonable_python would not be a
faithful gate here: it coerces a set or bytes rather than rejecting them, so it
would pass payloads json.dumps then refuses.
The check is deliberately not at the send. A transport failure means the peer
is unreachable and propagating it unanswered is correct, which
test_response_send_failure_is_not_mapped_to_a_handler_error asserts; an
encoding failure means the peer is reachable and the payload is our fault.
Catching around transport.send would conflate the two and would convert a
transport implementation's own TypeError into a protocol error reported to the
peer. That file is untouched here.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>1 parent 9853d8b commit 9e768fd
2 files changed
Lines changed: 81 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
283 | 283 | | |
284 | 284 | | |
285 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
286 | 296 | | |
287 | 297 | | |
288 | 298 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
975 | 975 | | |
976 | 976 | | |
977 | 977 | | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
0 commit comments