From f861178edc577ac0f2c5ad02ef2fcdf9de58ffde Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Sun, 11 Aug 2019 17:22:18 +0200 Subject: [PATCH] AsyncHttpConsumer: Do not stop the consumer when exiting `self.handle` already Earlier `self.handle` always disconnected the HTTP client and stopped the consumer when exiting. This meant that the consumer couldn't receive any messages from the channel layer at all, making it impossible to send e.g. messages from workers to connected HTTP clients via long polling or server-sent events. --- channels/generic/http.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/channels/generic/http.py b/channels/generic/http.py index d067717a2..ca5f470fb 100644 --- a/channels/generic/http.py +++ b/channels/generic/http.py @@ -43,6 +43,9 @@ async def send_body(self, body, *, more_body=False): await self.send( {"type": "http.response.body", "body": body, "more_body": more_body} ) + if not more_body: + await self.disconnect() + raise StopConsumer() async def send_response(self, status, body, **kwargs): """ @@ -78,11 +81,7 @@ async def http_request(self, message): if "body" in message: self.body.append(message["body"]) if not message.get("more_body"): - try: - await self.handle(b"".join(self.body)) - finally: - await self.disconnect() - raise StopConsumer() + await self.handle(b"".join(self.body)) async def http_disconnect(self, message): """