From ba2bbffd2053f49ed57a70556fa6435cccbc79e3 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sun, 14 Feb 2016 18:48:53 +0000 Subject: [PATCH] Improve HTTP cleanup and multi-response lock --- daphne/http_protocol.py | 13 +++++++++++-- daphne/server.py | 8 +++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 8187c6c..4c36e88 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -69,7 +69,7 @@ class WebRequest(http.Request): protocol.dataReceived(data) # Remove our HTTP reply channel association logger.debug("Upgraded connection %s to WebSocket %s", self.reply_channel, protocol.reply_channel) - self.factory.reply_protocols[self.reply_channel] = None + del self.factory.reply_protocols[self.reply_channel] self.reply_channel = None # Boring old HTTP. else: @@ -101,11 +101,20 @@ class WebRequest(http.Request): """ Cleans up reply channel on close. """ - if self.reply_channel: + if self.reply_channel and self.reply_channel in self.channel.factory.reply_protocols: del self.channel.factory.reply_protocols[self.reply_channel] logger.debug("HTTP disconnect for %s", self.reply_channel) http.Request.connectionLost(self, reason) + def finish(self): + """ + Cleans up reply channel on close. + """ + if self.reply_channel: + del self.channel.factory.reply_protocols[self.reply_channel] + logger.debug("HTTP close for %s", self.reply_channel) + http.Request.finish(self) + def serverResponse(self, message): """ Writes a received HTTP response back out to the transport. diff --git a/daphne/server.py b/daphne/server.py index daff148..fb3f9d8 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -1,8 +1,11 @@ +import logging import time from twisted.internet import reactor from .http_protocol import HTTPFactory +logger = logging.getLogger(__name__) + class Server(object): @@ -27,10 +30,13 @@ class Server(object): channels = self.factory.reply_channels() # Quit if reactor is stopping if not reactor.running: + logging.debug("Backend reader quitting due to reactor stop") return # Don't do anything if there's no channels to listen on if channels: - channel, message = self.channel_layer.receive_many(channels, block=True) + channel, message = self.channel_layer.receive_many(channels, block=False) + if channel: + logging.debug("Server got message on %s", channel) else: time.sleep(0.1) continue