Skip to content
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

Closed
glglgl mannequin opened this issue Mar 29, 2011 · 10 comments
Closed

threading.Semaphore does not use try...finally #55923

glglgl mannequin opened this issue Mar 29, 2011 · 10 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@glglgl
Copy link
Mannequin

glglgl mannequin commented Mar 29, 2011

BPO 11714
Nosy @pitrou, @serhiy-storchaka
Files
  • threading.patch: patch removing the said issue
  • threading.patch: Patch to be applied to 69f58be4688a
  • threading.b36cb4602e21.patch: Patch to be applied to b36cb4602e21
  • 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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2013-04-22.20:05:03.974>
    created_at = <Date 2011-03-29.20:08:05.003>
    labels = ['extension-modules', 'type-bug']
    title = 'threading.Semaphore does not use try...finally'
    updated_at = <Date 2013-04-22.20:05:03.972>
    user = 'https://bugs.python.org/glglgl'

    bugs.python.org fields:

    activity = <Date 2013-04-22.20:05:03.972>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2013-04-22.20:05:03.974>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules']
    creation = <Date 2011-03-29.20:08:05.003>
    creator = 'glglgl'
    dependencies = []
    files = ['21460', '21495', '21496']
    hgrepos = []
    issue_num = 11714
    keywords = ['patch']
    message_count = 10.0
    messages = ['132515', '132523', '132524', '132525', '132603', '132701', '132703', '181547', '187586', '187588']
    nosy_count = 4.0
    nosy_names = ['pitrou', 'python-dev', 'glglgl', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue11714'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @glglgl
    Copy link
    Mannequin Author

    glglgl mannequin commented Mar 29, 2011

    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.

    @glglgl glglgl mannequin added the extension-modules C modules in the Modules dir label Mar 29, 2011
    @glglgl
    Copy link
    Mannequin Author

    glglgl mannequin commented Mar 29, 2011

    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.

    @pitrou
    Copy link
    Member

    pitrou commented Mar 29, 2011

    It would be simpler to use "with" indeed.
    Do you want to provide a patch?

    @glglgl
    Copy link
    Mannequin Author

    glglgl mannequin commented Mar 29, 2011

    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...

    @glglgl glglgl mannequin added the type-bug An unexpected behavior, bug, or error label Mar 29, 2011
    @pitrou
    Copy link
    Member

    pitrou commented Mar 30, 2011

    What version of Python did you make your patch against? It failed applying against the default branch.
    You might want to make sure you are using an up-to-date source tree, see: http://docs.python.org/devguide/setup.html#getting-the-source-code

    @glglgl
    Copy link
    Mannequin Author

    glglgl mannequin commented Mar 31, 2011

    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.

    @glglgl
    Copy link
    Mannequin Author

    glglgl mannequin commented Mar 31, 2011

    Here is another patch which fits to 2.7, if desired.

    @serhiy-storchaka
    Copy link
    Member

    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/
    http://python.org/psf/contrib/

    @serhiy-storchaka serhiy-storchaka self-assigned this Feb 6, 2013
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 22, 2013

    New changeset 2253b8a18bbf by Serhiy Storchaka in branch '2.7':
    Issue bpo-11714: Use 'with' statements to assure a Semaphore releases a
    http://hg.python.org/cpython/rev/2253b8a18bbf

    New changeset af30c5cb248f by Serhiy Storchaka in branch '3.3':
    Issue bpo-11714: Use 'with' statements to assure a Semaphore releases a
    http://hg.python.org/cpython/rev/af30c5cb248f

    New changeset a26df2d03989 by Serhiy Storchaka in branch 'default':
    Issue bpo-11714: Use 'with' statements to assure a Semaphore releases a
    http://hg.python.org/cpython/rev/a26df2d03989

    @serhiy-storchaka
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants