From 67d68c2fc87a7c183186f74f7563c181d4ea206a Mon Sep 17 00:00:00 2001 From: Eygene Ryabinkin Date: Mon, 22 Jul 2013 00:08:39 +0400 Subject: [PATCH] IMAP: simplify locking and fix potential lock loss Run the locked code under 'with': this guarantees that lock will be released in any case. This modification also avoids the case when our thread wasn't running locked when exception was caught, another thread got the lock, our code checked it via self.connectionlock.locked() and errorneously released the lock thinking that is was running locked. Signed-off-by: Eygene Ryabinkin --- offlineimap/imapserver.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py index ef05cb7..75203e0 100644 --- a/offlineimap/imapserver.py +++ b/offlineimap/imapserver.py @@ -297,10 +297,9 @@ class IMAPServer: self.delim = imaputil.dequote(self.delim) self.root = imaputil.dequote(self.root) - self.connectionlock.acquire() - self.assignedconnections.append(imapobj) - self.lastowner[imapobj] = curThread.ident - self.connectionlock.release() + with self.connectionlock: + self.assignedconnections.append(imapobj) + self.lastowner[imapobj] = curThread.ident return imapobj except Exception as e: """If we are here then we did not succeed in getting a @@ -308,9 +307,6 @@ class IMAPServer: error...""" self.semaphore.release() - if(self.connectionlock.locked()): - self.connectionlock.release() - severity = OfflineImapError.ERROR.REPO if type(e) == gaierror: #DNS related errors. Abort Repo sync