From: Thomas Grainger Date: Sun, 29 Dec 2024 12:46:15 +0000 (+0000) Subject: collapse exceptions groups from streaming response X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=ae86e1d91d64c52f0db583ea2444cdcfb5472a92;p=thirdparty%2Fstarlette.git collapse exceptions groups from streaming response --- diff --git a/starlette/responses.py b/starlette/responses.py index 31874f655..c522e7f23 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -18,6 +18,7 @@ from urllib.parse import quote import anyio import anyio.to_thread +from starlette._utils import collapse_excgroups from starlette.background import BackgroundTask from starlette.concurrency import iterate_in_threadpool from starlette.datastructures import URL, Headers, MutableHeaders @@ -258,14 +259,15 @@ class StreamingResponse(Response): except OSError: raise ClientDisconnect() else: - async with anyio.create_task_group() as task_group: + with collapse_excgroups(): + async with anyio.create_task_group() as task_group: - async def wrap(func: typing.Callable[[], typing.Awaitable[None]]) -> None: - await func() - task_group.cancel_scope.cancel() + async def wrap(func: typing.Callable[[], typing.Awaitable[None]]) -> None: + await func() + task_group.cancel_scope.cancel() - task_group.start_soon(wrap, partial(self.stream_response, send)) - await wrap(partial(self.listen_for_disconnect, receive)) + task_group.start_soon(wrap, partial(self.stream_response, send)) + await wrap(partial(self.listen_for_disconnect, receive)) if self.background is not None: await self.background()