Commit a27bca7
committed
Harden ClientSession/dispatcher lifecycle and error handling
DirectDispatcher now mirrors JSONRPCDispatcher's lifecycle so the
public ClientSession(dispatcher=...) path honors the documented
contracts: send_raw_request raises RuntimeError before run() and
MCPError(CONNECTION_CLOSED) after close, inbound dispatch to a closed
side fails the peer's request instead of serving the exited session's
callbacks, and the ready-wait sits inside fail_after so a request
timeout bounds waiting on a peer whose run() never started.
ClientSession._on_notify self-contains a raising logging_callback /
message_handler (matching ServerRunner._on_notify) so a callback that
throws over DirectDispatcher costs only that delivery instead of
failing the in-process peer's notify() call.
JSONRPCDispatcher.notify is now fire-and-forget all the way: a post-
close send or a write onto a torn-down transport drops the
notification with a debug log instead of leaking raw anyio
ClosedResourceError/BrokenResourceError. DirectDispatcher.notify
matches.
JSONRPCDispatcher.send_raw_request: a builtin TimeoutError raised by a
custom transport's bounded send() now propagates raw instead of being
mislabelled REQUEST_TIMEOUT (the timeout-conversion arm only fires
once the response fail_after has armed). request_write_started is set
before the write so a delivered-but-cancelled request still triggers
the courtesy notifications/cancelled, and _handle_request skips the
shutdown CONNECTION_CLOSED answer once an answer write has started,
preferring possibly-zero responses over possibly-two for one id.
request_read_timeout_seconds=0.0 is now honored as fail-immediately on
both ClientSession.send_request and ServerSession.send_request (was
silently treated as unset via or-fallback). None remains the only
sentinel that defers to the session-level timeout.1 parent 32b76cf commit a27bca7
9 files changed
Lines changed: 657 additions & 64 deletions
File tree
- src/mcp
- client
- server
- tests
- client
- interaction/lowlevel
- server
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
229 | | - | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
| |||
243 | 247 | | |
244 | 248 | | |
245 | 249 | | |
246 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
247 | 255 | | |
248 | 256 | | |
249 | 257 | | |
| |||
541 | 549 | | |
542 | 550 | | |
543 | 551 | | |
544 | | - | |
545 | | - | |
546 | | - | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
547 | 562 | | |
548 | 563 | | |
549 | 564 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
87 | 97 | | |
88 | 98 | | |
89 | 99 | | |
| |||
93 | 103 | | |
94 | 104 | | |
95 | 105 | | |
96 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
97 | 109 | | |
98 | 110 | | |
99 | 111 | | |
| |||
104 | 116 | | |
105 | 117 | | |
106 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
107 | 127 | | |
108 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
109 | 134 | | |
110 | 135 | | |
111 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
112 | 143 | | |
113 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
114 | 148 | | |
115 | 149 | | |
116 | 150 | | |
| |||
120 | 154 | | |
121 | 155 | | |
122 | 156 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
128 | 176 | | |
129 | 177 | | |
130 | | - | |
| 178 | + | |
| 179 | + | |
131 | 180 | | |
132 | 181 | | |
133 | 182 | | |
| |||
142 | 191 | | |
143 | 192 | | |
144 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
145 | 212 | | |
146 | 213 | | |
147 | 214 | | |
148 | 215 | | |
149 | 216 | | |
150 | 217 | | |
151 | | - | |
152 | | - | |
153 | 218 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | 219 | | |
158 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
159 | 228 | | |
160 | 229 | | |
161 | 230 | | |
| |||
173 | 242 | | |
174 | 243 | | |
175 | 244 | | |
176 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
177 | 252 | | |
178 | 253 | | |
179 | 254 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
296 | 297 | | |
297 | 298 | | |
298 | 299 | | |
299 | | - | |
300 | | - | |
301 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
302 | 307 | | |
303 | 308 | | |
304 | 309 | | |
| |||
313 | 318 | | |
314 | 319 | | |
315 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
316 | 325 | | |
317 | 326 | | |
318 | 327 | | |
319 | 328 | | |
320 | 329 | | |
321 | | - | |
322 | 330 | | |
| 331 | + | |
323 | 332 | | |
324 | 333 | | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
325 | 340 | | |
326 | 341 | | |
327 | | - | |
| 342 | + | |
328 | 343 | | |
329 | 344 | | |
330 | 345 | | |
| |||
340 | 355 | | |
341 | 356 | | |
342 | 357 | | |
343 | | - | |
| 358 | + | |
344 | 359 | | |
345 | 360 | | |
346 | 361 | | |
| |||
365 | 380 | | |
366 | 381 | | |
367 | 382 | | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
368 | 392 | | |
369 | 393 | | |
370 | 394 | | |
371 | 395 | | |
372 | 396 | | |
373 | 397 | | |
374 | | - | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
375 | 403 | | |
376 | 404 | | |
377 | 405 | | |
| |||
613 | 641 | | |
614 | 642 | | |
615 | 643 | | |
| 644 | + | |
616 | 645 | | |
617 | 646 | | |
618 | 647 | | |
| |||
625 | 654 | | |
626 | 655 | | |
627 | 656 | | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
628 | 663 | | |
629 | 664 | | |
630 | 665 | | |
631 | 666 | | |
632 | 667 | | |
633 | 668 | | |
634 | 669 | | |
| 670 | + | |
635 | 671 | | |
636 | 672 | | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | | - | |
643 | | - | |
644 | | - | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
645 | 684 | | |
646 | 685 | | |
647 | 686 | | |
| |||
701 | 740 | | |
702 | 741 | | |
703 | 742 | | |
704 | | - | |
705 | | - | |
706 | | - | |
707 | | - | |
708 | | - | |
709 | | - | |
710 | | - | |
711 | | - | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
0 commit comments