Don't crash if connection closed before application started (#213)

Fixes #205.
This commit is contained in:
Brian May 2018-07-06 11:26:34 +10:00 committed by Andrew Godwin
parent 6dcc0d52b3
commit d5611bccb6
3 changed files with 6 additions and 3 deletions

View File

@ -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

View File

@ -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),

View File

@ -75,6 +75,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
"server": self.server_addr,
"subprotocols": subprotocols,
})
if self.application_deferred is not None:
self.application_deferred.addCallback(self.applicationCreateWorked)
self.application_deferred.addErrback(self.applicationCreateFailed)
except Exception as e: