Use lock with 'with' statement

To make sure, the lock gets released even if we raise an exception between
acquire() and release()

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2011-11-02 11:30:16 +01:00
parent d54859a931
commit ccfa747ce6

View File

@ -15,6 +15,7 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import with_statement # needed for python 2.5
from offlineimap import imaplibutil, imaputil, threadutil, OfflineImapError from offlineimap import imaplibutil, imaputil, threadutil, OfflineImapError
from offlineimap.ui import getglobalui from offlineimap.ui import getglobalui
from threading import Lock, BoundedSemaphore, Thread, Event, currentThread from threading import Lock, BoundedSemaphore, Thread, Event, currentThread
@ -355,7 +356,7 @@ class IMAPServer:
else: else:
# re-raise all other errors # re-raise all other errors
raise raise
def connectionwait(self): def connectionwait(self):
"""Waits until there is a connection available. Note that between """Waits until there is a connection available. Note that between
the time that a connection becomes available and the time it is the time that a connection becomes available and the time it is
@ -370,18 +371,18 @@ class IMAPServer:
def close(self): def close(self):
# Make sure I own all the semaphores. Let the threads finish # Make sure I own all the semaphores. Let the threads finish
# their stuff. This is a blocking method. # their stuff. This is a blocking method.
self.connectionlock.acquire() with self.connectionlock:
threadutil.semaphorereset(self.semaphore, self.maxconnections) # first, wait till all
for imapobj in self.assignedconnections + self.availableconnections: threadutil.semaphorereset(self.semaphore, self.maxconnections)
imapobj.logout() for imapobj in self.assignedconnections + self.availableconnections:
self.assignedconnections = [] imapobj.logout()
self.availableconnections = [] self.assignedconnections = []
self.lastowner = {} self.availableconnections = []
# reset kerberos state self.lastowner = {}
self.gss_step = self.GSS_STATE_STEP # reset kerberos state
self.gss_vc = None self.gss_step = self.GSS_STATE_STEP
self.gssapi = False self.gss_vc = None
self.connectionlock.release() self.gssapi = False
def keepalive(self, timeout, event): def keepalive(self, timeout, event):
"""Sends a NOOP to each connection recorded. It will wait a maximum """Sends a NOOP to each connection recorded. It will wait a maximum