From 13ef8b7d796025f412874349929296e01dab2672 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Wed, 2 Sep 2015 15:17:54 +0200 Subject: fix command server when dealing with very short lived connections, like --- voctocore/lib/controlserver.py | 17 ++++++++++++++--- voctocore/lib/tcpmulticonnection.py | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/voctocore/lib/controlserver.py b/voctocore/lib/controlserver.py index e0632fc..631770b 100644 --- a/voctocore/lib/controlserver.py +++ b/voctocore/lib/controlserver.py @@ -28,14 +28,17 @@ class ControlServer(TCPMultiConnection): def on_data(self, conn, _, leftovers, *args): '''Asynchronous connection handler. Pushes data from socket into command queue linewise''' + close_after = False try: while True: try: leftovers.append(conn.recv(4096).decode(errors='replace')) if len(leftovers[-1]) == 0: self.log.info("Socket was closed") - self.close_connection(conn) - return False + leftovers.pop() + close_after = True + break + except UnicodeDecodeError as e: continue except BlockingIOError as e: @@ -58,6 +61,10 @@ class ControlServer(TCPMultiConnection): self.command_queue.put((line, conn)) + if close_after: + self.close_connection(conn) + return False + self.log.debug("Remaining %r", lines[-1]) leftovers.append(lines[-1]) return True @@ -70,6 +77,9 @@ class ControlServer(TCPMultiConnection): line, requestor = self.command_queue.get() words = line.split() + if len(words) < 1: + return True + command = words[0] args = words[1:] @@ -99,7 +109,8 @@ class ControlServer(TCPMultiConnection): response = "%s\n" % str(responseObject) finally: - self.currentConnections[requestor].put(response) + if requestor in self.currentConnections: + self.currentConnections[requestor].put(response) return True diff --git a/voctocore/lib/tcpmulticonnection.py b/voctocore/lib/tcpmulticonnection.py index e6f8b5b..e9caf2c 100644 --- a/voctocore/lib/tcpmulticonnection.py +++ b/voctocore/lib/tcpmulticonnection.py @@ -37,5 +37,6 @@ class TCPMultiConnection(object): return True def close_connection(self, conn): - del(self.currentConnections[conn]) + if conn in self.currentConnections: + del(self.currentConnections[conn]) self.log.info('Now %u Receiver connected', len(self.currentConnections)) -- cgit v1.2.3