From 57db73454768d30f82805c1972df5c97fac65a36 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Thu, 23 Apr 2020 22:16:36 +0200 Subject: [PATCH] Workaround potential infinite loop in StreamingResponse --- starlette/responses.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/starlette/responses.py b/starlette/responses.py index 143e9e014..d993b1389 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -1,3 +1,4 @@ +import asyncio import hashlib import http.cookies import inspect @@ -202,6 +203,11 @@ async def listen_for_disconnect(self, receive: Receive) -> None: message = await receive() if message["type"] == "http.disconnect": break + # Relinquish control to the event loop to allow it to check if + # stream_response has completed. The receive() call above may not have + # handed control over to the loop, especially if there isn't actually a + # network between us and the client (e.g. when testing). + await asyncio.sleep(0) async def stream_response(self, send: Send) -> None: await send(