From 5a787f41d2527e936f2f6d2ae3da9e7f177a66cb Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Fri, 17 Nov 2017 13:37:51 +0100 Subject: [PATCH] Socket closing changes - explicit close only through asyncore error handler - implicit close through garbage collector - avoid duplicate closing --- src/network/advanceddispatcher.py | 6 +++--- src/network/bmproto.py | 3 +++ src/network/connectionpool.py | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 50ddf44b..c50b6a43 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -78,7 +78,7 @@ class AdvancedDispatcher(asyncore.dispatcher): self.uploadChunk = asyncore.uploadBucket self.uploadChunk = min(self.uploadChunk, len(self.write_buf)) return asyncore.dispatcher.writable(self) and \ - (self.connecting or self.uploadChunk > 0) + (self.connecting or (self.connected and self.uploadChunk > 0)) def readable(self): self.downloadChunk = AdvancedDispatcher._buf_len @@ -92,7 +92,7 @@ class AdvancedDispatcher(asyncore.dispatcher): except AttributeError: pass return asyncore.dispatcher.readable(self) and \ - (self.connecting or self.downloadChunk > 0) + (self.connecting or self.accepting or (self.connected and self.downloadChunk > 0)) def handle_read(self): self.lastTx = time.time() @@ -127,5 +127,5 @@ class AdvancedDispatcher(asyncore.dispatcher): self.read_buf = bytearray() with self.writeLock: self.write_buf = bytearray() - self.state = "close" + self.set_state("close") self.close() diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 9bc60af7..e4f7b406 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -546,6 +546,9 @@ class BMProto(AdvancedDispatcher, ObjectTracker): def handle_close(self): self.set_state("close") + if not (self.accepting or self.connecting or self.connected): + # already disconnected + return try: logger.debug("%s:%i: closing, %s", self.destination.host, self.destination.port, self.close_reason) except AttributeError: diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index ebde717b..aa48093e 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -214,11 +214,13 @@ class BMConnectionPool(object): else: if self.listeningSockets: for i in self.listeningSockets.values(): - i.handle_close() + i.close_reason = "Stopping listening" + i.accepting = i.connecting = i.connected = False logger.info('Stopped listening for incoming connections.') if self.udpSockets: for i in self.udpSockets.values(): - i.handle_close() + i.close_reason = "Stopping UDP socket" + i.accepting = i.connecting = i.connected = False logger.info('Stopped udp sockets.') loopTime = float(self.spawnWait) @@ -238,8 +240,6 @@ class BMConnectionPool(object): i.close_reason = "Timeout (%is)" % (time.time() - i.lastTx) i.set_state("close") for i in self.inboundConnections.values() + self.outboundConnections.values() + self.listeningSockets.values() + self.udpSockets.values(): - if i.state == "close": - i.handle_close() if not (i.accepting or i.connecting or i.connected): reaper.append(i) for i in reaper: