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 <rea@codelabs.ru>
This commit is contained in:
Eygene Ryabinkin 2013-07-22 00:08:39 +04:00
parent 41cb0f577f
commit 67d68c2fc8

View File

@ -297,10 +297,9 @@ class IMAPServer:
self.delim = imaputil.dequote(self.delim) self.delim = imaputil.dequote(self.delim)
self.root = imaputil.dequote(self.root) self.root = imaputil.dequote(self.root)
self.connectionlock.acquire() with self.connectionlock:
self.assignedconnections.append(imapobj) self.assignedconnections.append(imapobj)
self.lastowner[imapobj] = curThread.ident self.lastowner[imapobj] = curThread.ident
self.connectionlock.release()
return imapobj return imapobj
except Exception as e: except Exception as e:
"""If we are here then we did not succeed in getting a """If we are here then we did not succeed in getting a
@ -308,9 +307,6 @@ class IMAPServer:
error...""" error..."""
self.semaphore.release() self.semaphore.release()
if(self.connectionlock.locked()):
self.connectionlock.release()
severity = OfflineImapError.ERROR.REPO severity = OfflineImapError.ERROR.REPO
if type(e) == gaierror: if type(e) == gaierror:
#DNS related errors. Abort Repo sync #DNS related errors. Abort Repo sync