New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
threading.Semaphore does not use try...finally #55923
Comments
The acquire() and release() functions of threading.Semaphore do not make use of the try...finally suite as it would be reasonable. 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. |
I wonder if it is right what I wrote. After a second thought, the acquire() should come *after* try:, as well in threading.Event. Because if Ctrl-C is pressed while waiting in acquire(), a KeyboardInterrupt is thrown immediately after returning from acquire(). This lock must be release()d again in order to be consistent. |
It would be simpler to use "with" indeed. |
Of course. I hope it is correct, and I hop it is ok when I change to use "with ...:" on the other places I consider it useful as well... |
What version of Python did you make your patch against? It failed applying against the default branch. |
Oops, sorry. I have re-made it; it is to be applied to 69f58be4688a. The original one was made against the respective file of my distribution which contains Python 2.6. It can be applied to (at least) 787b969d37f0, a fact which might help backporting it to 2.x, if wanted. |
Here is another patch which fits to 2.7, if desired. |
LGTM. 3.x patch a little desynchronized but it is easy to update it. I'll commit it if Antoine have no objections. Thomas Rachel, can you please submit a contributor form? http://python.org/psf/contrib/contrib-form/ |
New changeset 2253b8a18bbf by Serhiy Storchaka in branch '2.7': New changeset af30c5cb248f by Serhiy Storchaka in branch '3.3': New changeset a26df2d03989 by Serhiy Storchaka in branch 'default': |
Thank you for the patch. 'try/finally' can't be replaced by 'with' statements in the _Event class because a conditional variable should be reinitialized in _reset_internal_locks() after fork. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: