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, "server": self.server_addr,
}) })
# Check they didn't close an unfinished request # 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. # Not much we can do, the request is prematurely abandoned.
return return
# Run application against request # Run application against request

View File

@ -185,6 +185,8 @@ class Server(object):
input_queue = asyncio.Queue() input_queue = asyncio.Queue()
application_instance = yield deferToThread(self.application, scope=scope) application_instance = yield deferToThread(self.application, scope=scope)
# Run it, and stash the future for later checking # 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( self.connections[protocol]["application_instance"] = asyncio.ensure_future(application_instance(
receive=input_queue.get, receive=input_queue.get,
send=lambda message: self.handle_reply(protocol, message), send=lambda message: self.handle_reply(protocol, message),

View File

@ -75,8 +75,9 @@ class WebSocketProtocol(WebSocketServerProtocol):
"server": self.server_addr, "server": self.server_addr,
"subprotocols": subprotocols, "subprotocols": subprotocols,
}) })
self.application_deferred.addCallback(self.applicationCreateWorked) if self.application_deferred is not None:
self.application_deferred.addErrback(self.applicationCreateFailed) self.application_deferred.addCallback(self.applicationCreateWorked)
self.application_deferred.addErrback(self.applicationCreateFailed)
except Exception as e: except Exception as e:
# Exceptions here are not displayed right, just 500. # Exceptions here are not displayed right, just 500.
# Turn them into an ERROR log. # Turn them into an ERROR log.