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
@ -370,7 +371,8 @@ 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:
# first, wait till all
threadutil.semaphorereset(self.semaphore, self.maxconnections) threadutil.semaphorereset(self.semaphore, self.maxconnections)
for imapobj in self.assignedconnections + self.availableconnections: for imapobj in self.assignedconnections + self.availableconnections:
imapobj.logout() imapobj.logout()
@ -381,7 +383,6 @@ class IMAPServer:
self.gss_step = self.GSS_STATE_STEP self.gss_step = self.GSS_STATE_STEP
self.gss_vc = None self.gss_vc = None
self.gssapi = False self.gssapi = False
self.connectionlock.release()
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