From f1555ec89311c1142f06a7085f9a097b0b2dccc8 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 1 Sep 2011 11:01:00 +0200 Subject: [PATCH] Catch terminated connections on the IDLE SELECT operation Handle the case gracefully where a server has closed an IMAP connection that we want to use for IDLEing. Simply have it dropped and get a new one in this case. THis should get rid of the errors reported by John Wiegley in mail id:"m2sjohd16t.fsf@gmail.com". Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/imapserver.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py index 3ce751c..e630f83 100644 --- a/offlineimap/imapserver.py +++ b/offlineimap/imapserver.py @@ -26,7 +26,7 @@ import socket import base64 import time import errno - +from sys import exc_info from socket import gaierror try: from ssl import SSLError, cert_time_to_seconds @@ -456,6 +456,7 @@ class IdleThread(object): self.parent = parent self.folder = folder self.event = Event() + self.ui = getglobalui() if folder is None: self.thread = Thread(target=self.noop) else: @@ -505,13 +506,24 @@ class IdleThread(object): self.imapaborted = True self.stop() - imapobj = self.parent.acquireconnection() - imapobj.select(self.folder) + success = False # successfully selected FOLDER? + while not success: + imapobj = self.parent.acquireconnection() + try: + imapobj.select(self.folder) + except OfflineImapError, e: + if e.severity == OfflineImapError.ERROR.FOLDER_RETRY: + # Connection closed, release connection and retry + self.ui.error(e, exc_info()[2]) + self.parent.releaseconnection(imapobj) + else: + raise e + else: + success = True if "IDLE" in imapobj.capabilities: imapobj.idle(callback=callback) else: - ui = getglobalui() - ui.warn("IMAP IDLE not supported on connection to %s." + self.ui.warn("IMAP IDLE not supported on connection to %s." "Falling back to old behavior: sleeping until next" "refresh cycle." %(imapobj.identifier,))