From 1184b232237925c831eec9785d91da6674196140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0urda?= Date: Mon, 19 Feb 2018 21:27:38 +0100 Subject: [PATCH] Separate protocol processing exceptions - exceptions thrown by the state methods are separated from missing connections or states. This allows more accurate error reporting --- src/network/advanceddispatcher.py | 16 ++++++++++++---- src/network/receivequeuethread.py | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 6f857398..d426dbe8 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -7,6 +7,12 @@ from debug import logger from helper_threading import BusyError, nonBlocking import state +class ProcessingError(Exception): + pass + +class UnknownStateError(ProcessingError): + pass + class AdvancedDispatcher(asyncore.dispatcher): _buf_len = 131072 # 128kB @@ -58,11 +64,13 @@ class AdvancedDispatcher(asyncore.dispatcher): break if len(self.read_buf) < self.expectBytes: return False - if not getattr(self, "state_" + str(self.state))(): + try: + cmd = getattr(self, "state_" + str(self.state)) + except AttributeError: + logger.error("Unknown state %s", self.state, exc_info=True) + raise UnknownState(self.state) + if not cmd(): break - except AttributeError: - logger.error("Unknown state %s", self.state, exc_info=True) - raise except BusyError: return False return False diff --git a/src/network/receivequeuethread.py b/src/network/receivequeuethread.py index 5399b972..0a7562cb 100644 --- a/src/network/receivequeuethread.py +++ b/src/network/receivequeuethread.py @@ -12,6 +12,7 @@ from helper_threading import StoppableThread from inventory import Inventory from network.connectionpool import BMConnectionPool from network.bmproto import BMProto +from network.advanceddispatcher import UnknownStateError from queues import receiveDataQueue import protocol import state @@ -40,14 +41,21 @@ class ReceiveQueueThread(threading.Thread, StoppableThread): # or the connection is to be aborted try: - BMConnectionPool().getConnectionByAddr(dest).process() + connection = BMConnectionPool().getConnectionByAddr(dest) # KeyError = connection object not found - # AttributeError = state isn't implemented - except (KeyError, AttributeError): + except KeyError: + receiveDataQueue.task_done() + continue + try: + connection.process() + # UnknownStateError = state isn't implemented + except (UnknownStateError): pass except socket.error as err: if err.errno == errno.EBADF: - BMConnectionPool().getConnectionByAddr(dest).set_state("close", 0) + connection.set_state("close", 0) else: logger.error("Socket error: %s", str(err)) + except: + logger.error("Error processing", exc_info=True) receiveDataQueue.task_done()