]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
collapse exceptions groups from streaming response
authorThomas Grainger <tagrain@gmail.com>
Sun, 29 Dec 2024 12:46:15 +0000 (12:46 +0000)
committerThomas Grainger <tagrain@gmail.com>
Sun, 29 Dec 2024 14:09:17 +0000 (14:09 +0000)
starlette/responses.py

index 31874f655b96be7e36ddb67faaf4c3bd223f5372..c522e7f23b9710affc0f38fcd358790898f2eef6 100644 (file)
@@ -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()