Skip to content

Documentation for Event.wait return value is either wrong or incomplete #57711

Closed

Description

@bitdancer
BPO 13502
Nosy @tim-one, @pitrou, @bitdancer
Files
  • event_wait_cleared.diff
  • event_wait_cleared-1.diff
  • 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 = None
    closed_at = <Date 2012-01-07.19:40:56.442>
    created_at = <Date 2011-11-29.20:45:15.541>
    labels = ['type-bug', 'library']
    title = 'Documentation for Event.wait return value is either wrong or incomplete'
    updated_at = <Date 2012-01-07.19:40:56.441>
    user = 'https://github.com/bitdancer'

    bugs.python.org fields:

    activity = <Date 2012-01-07.19:40:56.441>
    actor = 'neologix'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2012-01-07.19:40:56.442>
    closer = 'neologix'
    components = ['Library (Lib)']
    creation = <Date 2011-11-29.20:45:15.541>
    creator = 'r.david.murray'
    dependencies = []
    files = ['23866', '23897']
    hgrepos = []
    issue_num = 13502
    keywords = ['patch']
    message_count = 12.0
    messages = ['148604', '148606', '148608', '148610', '148641', '148984', '149018', '149019', '149130', '149150', '149160', '150809']
    nosy_count = 6.0
    nosy_names = ['tim.peters', 'pitrou', 'r.david.murray', 'neologix', 'docs@python', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13502'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    Activity

    1. bitdancer commented on Nov 29, 2011

      @bitdancer
      MemberAuthor

      The documentation for Event.wait says:

      This method returns the internal flag on exit, so it will always return True except if a timeout is given and the operation times out.

      In fact, however, if the thread that sets the flag immediately clears it, wait will return False. Antoine looking at the code says that this appears to be intentional, and that would make sense since originally wait returned no value.

      My use case is one thread waiting on another to complete a work loop. Normally the worker thread goes to sleep after clearing the flag, but sometimes it immediately starts a new work loop. In either case I want the monitoring loop to take an action when the work loop completes, and raise an error if the wait times out. It looked to me like Event.wait would work in the scenario.

    2. bitdancer commented on Nov 29, 2011

      @bitdancer
      MemberAuthor

      Thinking about it, if the flag's state if the wait does not expire is not guaranteed to be True, then what I really need is some way to know, when the wait call completes, whether or not it terminated because of a timeout or not. I can always query the value of the flag separately if its value can in any case be changed by another thread.

      I am perhaps using the wrong tool, I'll have to look at the docs further.

    3. pitrou commented on Nov 29, 2011

      @pitrou
      Member

      Ok, so digging a bit, the feature dates back to bpo-1674032.
      Interestingly, the OP in that issue had already envisioned that race condition and had precisely proposed the feature to avoid it: “Note that in the above case, the event could be cleared between the return from x.wait and the execution of x.isSet (in another thread), and so this would operate as though x.wait had just timed out”.

      So I'm changing my mind and saying it seems desireable to return True if the wait succeeded before the timeout's end, even though the flag may have been reset in the meantime.
      In 3.2, Condition.wait also returns a boolean, meaning the check in Event.wait can be atomic.

    4. bitdancer commented on Nov 29, 2011

      @bitdancer
      MemberAuthor

      Changing the title and component to reflect Antoine's new understanding :)

    5. added
      stdlibStandard Library Python modules in the Lib/ directory
      and removed
      docsDocumentation in the Doc dir
      on Nov 29, 2011
    6. changed the title [-]Documentation for Event.wait return value is either wrong or incomplete[/-] [+]Event.wait sometimes returns False even when no timeout has occurred[/+] on Nov 29, 2011
    7. neologix commented on Nov 30, 2011

      neologixmannequin
      Mannequin

      So I'm changing my mind and saying it seems desireable to return True if the wait succeeded before the timeout's end, even though the flag may have been reset in the meantime.

      Agreed. What matters is that the event has been signaled, not that
      it's currently signaled.

    8. changed the title [-]Event.wait sometimes returns False even when no timeout has occurred[/-] [+]Documentation for Event.wait return value is either wrong or incomplete[/+] on Nov 30, 2011
    9. neologix commented on Dec 7, 2011

      neologixmannequin
      Mannequin

      Here's a patch.

    10. bitdancer commented on Dec 8, 2011

      @bitdancer
      MemberAuthor

      On Wed, 07 Dec 2011 18:45:27 +0000, report@bugs.python.org wrote:


      diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst
      --- a/Doc/library/threading.rst
      +++ b/Doc/library/threading.rst
      @@ -804,8 +804,9 @@
      floating point number specifying a timeout for the operation in seconds
      (or fractions thereof).

      •  This method returns the internal flag on exit, so it will always return
        
      •  ``True`` except if a timeout is given and the operation times out.
        
      •  This method returns true if and only if the internal flag has been set to
        
      •  true by another thread, so it will always return ``True`` except if a
        
      •  timeout is given and the operation times out.
        

      This seems to imply that if the current thread previously set the event,
      the wait will return False, which is contradicted by the 'so' statement
      (which appears to be correct).

      --David

    11. neologix commented on Dec 8, 2011

      neologixmannequin
      Mannequin

      This seems to imply that if the current thread previously set the event,
      the wait will return False, which is contradicted by the 'so' statement
      (which appears to be correct).

      You're probably referring to the "another thread" part...
      A suggestion ? I see you're a native speaker :-)

    12. bitdancer commented on Dec 9, 2011

      @bitdancer
      MemberAuthor

      "set to True, either before the wait call or after the wait starts"

    13. neologix commented on Dec 10, 2011

      neologixmannequin
      Mannequin

      Thanks, here's a patch updated accordingly.

    14. bitdancer commented on Dec 10, 2011

      @bitdancer
      MemberAuthor

      LGTM.

    15. python-dev commented on Jan 7, 2012

      python-devmannequin
      Mannequin

      New changeset eb39d862a250 by Charles-François Natali in branch '3.2':
      Issue bpo-13502: threading: Fix a race condition in Event.wait() that made it
      http://hg.python.org/cpython/rev/eb39d862a250

      New changeset 0fe63bb20e74 by Charles-François Natali in branch 'default':
      Issue bpo-13502: threading: Fix a race condition in Event.wait() that made it
      http://hg.python.org/cpython/rev/0fe63bb20e74

    16. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions