From c4125c66d415443de777a2a3f80f79aafc4a9983 Mon Sep 17 00:00:00 2001 From: Kyle Agronick Date: Tue, 27 Nov 2018 15:20:27 -0500 Subject: [PATCH] Only set disconnected time when it is not already set (#237) Fixes a memory leak where the time would never expire, as well as an additional case where send is called on an already-cleaned-up instance. --- daphne/server.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/daphne/server.py b/daphne/server.py index 3f27bf5..2dbe476 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -176,7 +176,11 @@ class Server(object): def protocol_disconnected(self, protocol): # Set its disconnected time (the loops will come and clean it up) - self.connections[protocol]["disconnected"] = time.time() + # Do not set it if it is already set. Overwriting it might + # cause it to never be cleaned up. + # See https://github.com/django/channels/issues/1181 + if "disconnected" not in self.connections[protocol]: + self.connections[protocol]["disconnected"] = time.time() ### Internal event/message handling @@ -208,8 +212,10 @@ class Server(object): """ Coroutine that jumps the reply message from asyncio to Twisted """ - # Don't do anything if the connection is closed - if self.connections[protocol].get("disconnected", None): + # Don't do anything if the connection is closed or does not exist + if protocol not in self.connections or self.connections[protocol].get( + "disconnected", None + ): return self.check_headers_type(message) # Let the protocol handle it