Commit 1d56c4a
committed
review(#2484): restore exception propagation from background
reader/writer and close streams on crash
Self-review of the first commit caught a regression: the asyncio
branch spawned stdout_reader / stdin_writer via asyncio.create_task
and did `try: await task except BaseException: pass` during cleanup,
which silently swallowed any exception the reader or writer raised.
An anyio task group would have re-raised that through the `async
with` block — the asyncio path has to reproduce that contract to
keep the fix observably equivalent.
Two changes:
1. `add_done_callback(_on_background_task_done)` — if a background
task crashes while the caller is still inside the `yield`, close
the memory streams immediately so any in-flight user read wakes
up with `ClosedResourceError` instead of hanging forever. Mirrors
the scope-cancellation side effect of a task group. The callback
logs the exception at debug so operators can trace crashes that
we only surface on the way out.
2. On `__aexit__` we now collect task results and re-raise the first
non-cancellation, non-closed-resource exception. CancelledError
and anyio.ClosedResourceError are expected during the teardown we
just forced in `_cleanup_process_and_streams`, so they are
swallowed; anything else (a real crash) propagates.
New regression test:
`test_stdio_client_reader_crash_propagates_on_asyncio` — injects a
TextReceiveStream that raises on the first `__anext__`, asserts the
exception is visible at the async-with exit. Confirmed it FAILS on
the first-commit version ("Failed: DID NOT RAISE"), so it is real
regression coverage, not a tautology.
All 14 stdio tests pass (10 pre-existing + 4 new, 1 Unix-only skip).
Full `tests/client/` pass (203 passed, 1 pre-existing xfail).1 parent 42cf8aa commit 1d56c4a
2 files changed
+85
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
218 | 244 | | |
219 | | - | |
220 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
221 | 250 | | |
222 | 251 | | |
223 | 252 | | |
224 | 253 | | |
225 | 254 | | |
226 | 255 | | |
227 | | - | |
| 256 | + | |
228 | 257 | | |
229 | 258 | | |
230 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
231 | 268 | | |
232 | 269 | | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
237 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
238 | 279 | | |
239 | 280 | | |
240 | 281 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
0 commit comments