From 325a7586e113f7603d50b273de6f2752c19e9620 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sat, 21 Sep 2024 23:50:03 +0200 Subject: [PATCH 1/2] Raise exception from background task --- starlette/middleware/base.py | 37 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/starlette/middleware/base.py b/starlette/middleware/base.py index f51b13f73..cd3851bca 100644 --- a/starlette/middleware/base.py +++ b/starlette/middleware/base.py @@ -60,32 +60,20 @@ async def wrapped_receive(self) -> Message: if getattr(self, "_body", None) is not None: # body() was called, we return it even if the client disconnected self._wrapped_rcv_consumed = True - return { - "type": "http.request", - "body": self._body, - "more_body": False, - } + return {"type": "http.request", "body": self._body, "more_body": False} elif self._stream_consumed: # stream() was called to completion # return an empty body so that downstream apps don't hang # waiting for a disconnect self._wrapped_rcv_consumed = True - return { - "type": "http.request", - "body": b"", - "more_body": False, - } + return {"type": "http.request", "body": b"", "more_body": False} else: # body() was never called and stream() wasn't consumed try: stream = self.stream() chunk = await stream.__anext__() self._wrapped_rcv_consumed = self._stream_consumed - return { - "type": "http.request", - "body": chunk, - "more_body": not self._stream_consumed, - } + return {"type": "http.request", "body": chunk, "more_body": not self._stream_consumed} except ClientDisconnect: self._wrapped_rcv_disconnected = True return {"type": "http.disconnect"} @@ -148,6 +136,8 @@ async def coro() -> None: try: await self.app(scope, receive_or_disconnect, send_no_error) except Exception as exc: + # import traceback + # traceback.print_exc() app_exc = exc task_group.start_soon(close_recv_stream_on_response_sent) @@ -175,6 +165,8 @@ async def body_stream() -> typing.AsyncGenerator[bytes, None]: if not message.get("more_body", False): break + await anyio.sleep(0) + if app_exc is not None: raise app_exc @@ -211,13 +203,7 @@ def __init__( async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if self.info is not None: await send({"type": "http.response.debug", "info": self.info}) - await send( - { - "type": "http.response.start", - "status": self.status_code, - "headers": self.raw_headers, - } - ) + await send({"type": "http.response.start", "status": self.status_code, "headers": self.raw_headers}) async for chunk in self.body_iterator: await send({"type": "http.response.body", "body": chunk, "more_body": True}) @@ -225,4 +211,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: await send({"type": "http.response.body", "body": b"", "more_body": False}) if self.background: - await self.background() + try: + await self.background() + except Exception: + print("hi there") + breakpoint() + raise From 0b9449589a749a102c9dd2fcf8ac6f78909e615a Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sat, 21 Sep 2024 23:53:08 +0200 Subject: [PATCH 2/2] Remove code --- starlette/middleware/base.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/starlette/middleware/base.py b/starlette/middleware/base.py index cd3851bca..1e2ef5c8e 100644 --- a/starlette/middleware/base.py +++ b/starlette/middleware/base.py @@ -211,9 +211,4 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: await send({"type": "http.response.body", "body": b"", "more_body": False}) if self.background: - try: - await self.background() - except Exception: - print("hi there") - breakpoint() - raise + await self.background()