[issue11714] threading.Semaphore does not use try...finally

Thomas Rachel report at bugs.python.org
Tue Mar 29 22:08:05 CEST 2011


New submission from Thomas Rachel <th.rachel at googlemail.com>:

The acquire() and release() functions of threading.Semaphore do not make use of the try...finally suite as it would be reasonable.
They just do 

    def acquire(self, blocking=1):
        rc = False
        self.__cond.acquire()
        [...]
        self.__cond.release()
        return rc

    [...]

    def release(self):
        self.__cond.acquire()
        [...]
        self.__cond.release()

while IMO it would be appropriate to put a try: after the acquire() calls and a finally: before the release() calls so the lock is not held forever if an exception occurs.

(Feel free to use with self.__cond: instead...)

Especially when Ctrl-C is pressed while acquire() waits, the respective KeyboardInterrupt gets thrown after acquire(), breaking the respective function and the __cond is locked forever because it is never release()d.

----------
components: Extension Modules
messages: 132515
nosy: glglgl
priority: normal
severity: normal
status: open
title: threading.Semaphore does not use try...finally
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11714>
_______________________________________


More information about the Python-bugs-list mailing list