From a77ff7202a2862e73891b931eac4c31e342a6c34 Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Sat, 16 Mar 2024 16:23:17 +0900 Subject: [PATCH 1/2] chore: revert #2220 --- tests/protocols/test_http.py | 20 -------------------- uvicorn/protocols/http/h11_impl.py | 5 +---- uvicorn/protocols/http/httptools_impl.py | 22 +++++++++++----------- 3 files changed, 12 insertions(+), 35 deletions(-) diff --git a/tests/protocols/test_http.py b/tests/protocols/test_http.py index d570f8c39..1c3ead71e 100644 --- a/tests/protocols/test_http.py +++ b/tests/protocols/test_http.py @@ -16,7 +16,6 @@ from uvicorn.lifespan.on import LifespanOn from uvicorn.main import ServerState from uvicorn.protocols.http.h11_impl import H11Protocol -from uvicorn.protocols.utils import ClientDisconnected try: from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol @@ -587,25 +586,6 @@ async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable assert got_disconnect_event -@pytest.mark.anyio -async def test_disconnect_on_send(http_protocol_cls: HTTPProtocol) -> None: - got_disconnected = False - - async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable): - try: - await send({"type": "http.response.start", "status": 200}) - except ClientDisconnected: - nonlocal got_disconnected - got_disconnected = True - - protocol = get_connected_protocol(app, http_protocol_cls) - protocol.data_received(SIMPLE_GET_REQUEST) - protocol.eof_received() - protocol.connection_lost(None) - await protocol.loop.run_one() - assert got_disconnected - - @pytest.mark.anyio async def test_early_response(http_protocol_cls: HTTPProtocol): app = Response("Hello, world", media_type="text/plain") diff --git a/uvicorn/protocols/http/h11_impl.py b/uvicorn/protocols/http/h11_impl.py index 1e1872ef6..d0f2b2a5e 100644 --- a/uvicorn/protocols/http/h11_impl.py +++ b/uvicorn/protocols/http/h11_impl.py @@ -27,7 +27,6 @@ service_unavailable, ) from uvicorn.protocols.utils import ( - ClientDisconnected, get_client_addr, get_local_addr, get_path_with_query_string, @@ -408,8 +407,6 @@ async def run_asgi(self, app: ASGI3Application) -> None: result = await app( # type: ignore[func-returns-value] self.scope, self.receive, self.send ) - except ClientDisconnected: - pass except BaseException as exc: msg = "Exception in ASGI application\n" self.logger.error(msg, exc_info=exc) @@ -458,7 +455,7 @@ async def send(self, message: ASGISendEvent) -> None: await self.flow.drain() if self.disconnected: - raise ClientDisconnected + return if not self.response_started: # Sending response status line and headers diff --git a/uvicorn/protocols/http/httptools_impl.py b/uvicorn/protocols/http/httptools_impl.py index 2950bd537..53c34dc49 100644 --- a/uvicorn/protocols/http/httptools_impl.py +++ b/uvicorn/protocols/http/httptools_impl.py @@ -15,6 +15,7 @@ ASGI3Application, ASGIReceiveEvent, ASGISendEvent, + HTTPDisconnectEvent, HTTPRequestEvent, HTTPResponseBodyEvent, HTTPResponseStartEvent, @@ -29,7 +30,6 @@ service_unavailable, ) from uvicorn.protocols.utils import ( - ClientDisconnected, get_client_addr, get_local_addr, get_path_with_query_string, @@ -412,8 +412,6 @@ async def run_asgi(self, app: ASGI3Application) -> None: result = await app( # type: ignore[func-returns-value] self.scope, self.receive, self.send ) - except ClientDisconnected: - pass except BaseException as exc: msg = "Exception in ASGI application\n" self.logger.error(msg, exc_info=exc) @@ -462,7 +460,7 @@ async def send(self, message: ASGISendEvent) -> None: await self.flow.drain() if self.disconnected: - raise ClientDisconnected + return if not self.response_started: # Sending response status line and headers @@ -571,13 +569,15 @@ async def receive(self) -> ASGIReceiveEvent: await self.message_event.wait() self.message_event.clear() + message: HTTPDisconnectEvent | HTTPRequestEvent if self.disconnected or self.response_complete: - return {"type": "http.disconnect"} + message = {"type": "http.disconnect"} + else: + message = { + "type": "http.request", + "body": self.body, + "more_body": self.more_body, + } + self.body = b"" - message: HTTPRequestEvent = { - "type": "http.request", - "body": self.body, - "more_body": self.more_body, - } - self.body = b"" return message From de2d9d4a57d2a8fc0adef30da8200d078803cd2f Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 19 Mar 2024 09:06:04 +0100 Subject: [PATCH 2/2] Don't revert the receive changes --- uvicorn/protocols/http/httptools_impl.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/uvicorn/protocols/http/httptools_impl.py b/uvicorn/protocols/http/httptools_impl.py index 53c34dc49..997c6bb4c 100644 --- a/uvicorn/protocols/http/httptools_impl.py +++ b/uvicorn/protocols/http/httptools_impl.py @@ -15,7 +15,6 @@ ASGI3Application, ASGIReceiveEvent, ASGISendEvent, - HTTPDisconnectEvent, HTTPRequestEvent, HTTPResponseBodyEvent, HTTPResponseStartEvent, @@ -569,15 +568,8 @@ async def receive(self) -> ASGIReceiveEvent: await self.message_event.wait() self.message_event.clear() - message: HTTPDisconnectEvent | HTTPRequestEvent if self.disconnected or self.response_complete: - message = {"type": "http.disconnect"} - else: - message = { - "type": "http.request", - "body": self.body, - "more_body": self.more_body, - } - self.body = b"" - + return {"type": "http.disconnect"} + message: HTTPRequestEvent = {"type": "http.request", "body": self.body, "more_body": self.more_body} + self.body = b"" return message