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