Properly close connection if it has been reset by remote peer

This commit is contained in:
Patrick Ulbrich
2016-09-17 12:54:47 +02:00
parent 0d0b66c3c0
commit 36bbff2079
2 changed files with 9 additions and 11 deletions

View File

@@ -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)

View File

@@ -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)