From d5611bccb64e2aa1e2c3e77d2c401f6146c8d9af Mon Sep 17 00:00:00 2001 From: Brian May Date: Fri, 6 Jul 2018 11:26:34 +1000 Subject: [PATCH] Don't crash if connection closed before application started (#213) Fixes #205. --- daphne/http_protocol.py | 2 +- daphne/server.py | 2 ++ daphne/ws_protocol.py | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 1155135..3ff10be 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -160,7 +160,7 @@ class WebRequest(http.Request): "server": self.server_addr, }) # Check they didn't close an unfinished request - if self.content.closed: + if self.application_queue is None or self.content.closed: # Not much we can do, the request is prematurely abandoned. return # Run application against request diff --git a/daphne/server.py b/daphne/server.py index d8d43f2..63d34db 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -185,6 +185,8 @@ class Server(object): input_queue = asyncio.Queue() application_instance = yield deferToThread(self.application, scope=scope) # Run it, and stash the future for later checking + if protocol not in self.connections: + return None self.connections[protocol]["application_instance"] = asyncio.ensure_future(application_instance( receive=input_queue.get, send=lambda message: self.handle_reply(protocol, message), diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index 9edfce6..a7331b5 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -75,8 +75,9 @@ class WebSocketProtocol(WebSocketServerProtocol): "server": self.server_addr, "subprotocols": subprotocols, }) - self.application_deferred.addCallback(self.applicationCreateWorked) - self.application_deferred.addErrback(self.applicationCreateFailed) + if self.application_deferred is not None: + self.application_deferred.addCallback(self.applicationCreateWorked) + self.application_deferred.addErrback(self.applicationCreateFailed) except Exception as e: # Exceptions here are not displayed right, just 500. # Turn them into an ERROR log.