Skip to content

Instantly share code, notes, and snippets.

@kwirk
Created May 11, 2013 16:42
Show Gist options
  • Save kwirk/5560548 to your computer and use it in GitHub Desktop.
Save kwirk/5560548 to your computer and use it in GitHub Desktop.
fail2ban/fail2ban issue#161
diff --git a/server/asyncserver.py b/server/asyncserver.py
index 62a5dd8..02fc766 100644
--- a/server/asyncserver.py
+++ b/server/asyncserver.py
@@ -75,6 +75,10 @@ class RequestHandler(asynchat.async_chat):
logSys.error(traceback.format_exc().splitlines())
self.close()
+ def close(self):
+ self.socket.shutdown(socket.SHUT_RDWR)
+ asynchat.async_chat.close(self)
+
##
# Asynchronous server class.
#
@@ -95,6 +99,12 @@ class AsyncServer(asyncore.dispatcher):
def writable(self):
return False
+ ##
+ # Return True if socket initiliased and we've not closed the socket
+
+ def readable(self):
+ return self.__init
+
def handle_accept(self):
try:
conn, addr = self.accept()
@@ -136,14 +146,7 @@ class AsyncServer(asyncore.dispatcher):
self.listen(1)
# Sets the init flag.
self.__init = True
- # TODO Add try..catch
- # There's a bug report for Python 2.6/3.0 that use_poll=True yields some 2.5 incompatibilities:
- if sys.version_info >= (2, 6): # if python 2.6 or greater...
- logSys.debug("Detected Python 2.6 or greater. asyncore.loop() not using poll")
- asyncore.loop(use_poll = False) # fixes the "Unexpected communication problem" issue on Python 2.6 and 3.0
- else: # pragma: no cover
- logSys.debug("NOT Python 2.6/3.* - asyncore.loop() using poll")
- asyncore.loop(use_poll = True)
+ asyncore.loop(use_poll = True)
##
# Stops the communication server.
@@ -151,6 +154,7 @@ class AsyncServer(asyncore.dispatcher):
def stop(self):
if self.__init:
# Only closes the socket if it was initialized first.
+ self.__init = False
self.close()
# Remove socket
if os.path.exists(self.__sock):
@@ -158,6 +162,10 @@ class AsyncServer(asyncore.dispatcher):
os.remove(self.__sock)
logSys.debug("Socket shutdown")
+ def close(self):
+ self.socket.shutdown(socket.SHUT_RDWR)
+ asyncore.dispatcher.close(self)
+
##
# Marks socket as close-on-exec to avoid leaking file descriptors when
# running actions involving command execution.
diff --git a/testcases/sockettestcase.py b/testcases/sockettestcase.py
index d70c45c..87b5ef9 100644
--- a/testcases/sockettestcase.py
+++ b/testcases/sockettestcase.py
@@ -58,7 +58,7 @@ class Socket(unittest.TestCase):
self.assertEqual(client.send(testMessage), testMessage)
self.server.stop()
- serverThread.join(1)
+ serverThread.join()
self.assertFalse(os.path.exists(self.sock_name))
def testSocketForce(self):
@@ -75,5 +75,5 @@ class Socket(unittest.TestCase):
time.sleep(1)
self.server.stop()
- serverThread.join(1)
+ serverThread.join()
self.assertFalse(os.path.exists(self.sock_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment