From 36bbff20793322f8a6035a4ba6847c787a589643 Mon Sep 17 00:00:00 2001 From: Patrick Ulbrich Date: Sat, 17 Sep 2016 12:54:47 +0200 Subject: [PATCH] Properly close connection if it has been reset by remote peer --- Mailnag/backends/imap.py | 18 +++++++++--------- Mailnag/daemon/idlers.py | 2 -- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Mailnag/backends/imap.py b/Mailnag/backends/imap.py index 74dc0c8..30dd15d 100644 --- a/Mailnag/backends/imap.py +++ b/Mailnag/backends/imap.py @@ -49,7 +49,6 @@ class IMAPMailboxBackend(MailboxBackend): self.ssl = ssl # bool self.folders = folders self._conn = None - self._conn_closed = True def open(self): @@ -89,8 +88,6 @@ class IMAPMailboxBackend(MailboxBackend): except: pass raise # re-throw exception - self._conn_closed = False - # notify_next_change() (IMAP IDLE) requires a selected folder if self._conn.state == AUTH: self._select() @@ -99,9 +96,7 @@ class IMAPMailboxBackend(MailboxBackend): def close(self): # if conn has already been closed, don't try to close it again if self._conn != None: - if not self._conn_closed: - self._conn.close() - self._conn_closed = True + self._conn.close() self._conn.logout() self._conn = None @@ -109,8 +104,7 @@ class IMAPMailboxBackend(MailboxBackend): def is_open(self): return (self._conn != None) and \ (self._conn.state != imaplib.LOGOUT) and \ - (not self._conn.Terminate) and \ - (not self._conn_closed) + (not self._conn.Terminate) def list_messages(self): @@ -185,7 +179,13 @@ class IMAPMailboxBackend(MailboxBackend): # idle callback (runs on a further thread) def _idle_callback(args): # check if the connection has been reset by provider - self._conn_closed = (args[2] != None) and (args[2][0] is self._conn.abort) + if (args[2] != None) and (args[2][0] is self._conn.abort): + # conn has already been closed, don't try to close it again + # self._conn.close() # (calls idle_callback) + + # shutdown existing callback thread + self._conn.logout() + self._conn = None # call actual callback callback(args) diff --git a/Mailnag/daemon/idlers.py b/Mailnag/daemon/idlers.py index df4eb38..8bbb413 100644 --- a/Mailnag/daemon/idlers.py +++ b/Mailnag/daemon/idlers.py @@ -104,8 +104,6 @@ class Idler(object): def _reconnect(self): # connection has been reset by provider -> try to reconnect logging.info("Idler thread for account '%s' has been disconnected" % self._account.name) - - self._account.close() while (not self._account.is_open()) and (not self._event.isSet()): logging.info("Trying to reconnect Idler thread for account '%s'." % self._account.name)