Skip to content

Python threading event wait influenced by date change #79928

Description

@idok
mannequin
BPO 35747
Nosy @vstinner, @pablogsal
Superseder
  • bpo-12822: threading.Condition.wait(timeout) should use a monotonic clock: use pthread_condattr_setclock(CLOCK_MONOTONIC)
  • Files
  • wrong_wait_behaviour.py
  • 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 2019-01-17.10:06:55.688>
    created_at = <Date 2019-01-15.17:54:33.034>
    labels = ['type-bug', 'library']
    title = 'Python threading event wait influenced by date change'
    updated_at = <Date 2021-10-01.08:49:34.583>
    user = 'https://bugs.python.org/idok'

    bugs.python.org fields:

    activity = <Date 2021-10-01.08:49:34.583>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-01-17.10:06:55.688>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2019-01-15.17:54:33.034>
    creator = 'ido k'
    dependencies = []
    files = ['48056']
    hgrepos = []
    issue_num = 35747
    keywords = []
    message_count = 7.0
    messages = ['333718', '333719', '333720', '333796', '333811', '333842', '333843']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'pablogsal', 'ido k']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '12822'
    type = 'behavior'
    url = 'https://bugs.python.org/issue35747'
    versions = ['Python 3.6']

    Activity

    1. idok commented on Jan 15, 2019

      idokmannequin
      MannequinAuthor

      Happen on ubuntu

      Opening two threads - one thread alternate system date
      The seconds waits for 60 seconds. joining both threads.

      The execution should take at least 60 seconds. Takes less then 15 seconds.

      Any work around?

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jan 15, 2019
    3. vstinner commented on Jan 15, 2019

      @vstinner
      Member

      Python 3 uses a monotonic clock to implement timeouts, such clock is not affected by system clock changes *on purpose*. See time.monotonic() and PEP-418:
      https://docs.python.org/dev/library/time.html#time.monotonic
      https://www.python.org/dev/peps/pep-0418/

      Relying on the system clock can cause severe bugs.

      I suggest to close this issue as "not a bug".

    4. idok commented on Jan 15, 2019

      idokmannequin
      MannequinAuthor

      thanks for the comment

      please look at the code.
      i use wait on event for 60 seconds.
      the wait timed out in less than 60 seconds...

      why this is not a bug?

    5. pablogsal commented on Jan 16, 2019

      @pablogsal
      Member

      I think this is becase at the end, the threading code calls

      sem_timedwait(thelock, &ts) where &ts is the timespect structure. The manpage of sem_timedwait says:

      ...

      The timeout shall expire when the absolute time specified by abs_timeout passes, as measured by the clock on which timeouts are based (that is, when the value of that clock equals or exceeds abs_timeout), or if the absolute time specified by abs_timeout has already been passed at the time of the call.

      If the Timers option is supported, the timeout shall be based on the CLOCK_REALTIME clock. If the Timers option is not supported, the timeout shall be based on the system clock as returned by the time() function. The resolution of the timeout shall be the resolution of the clock on which it is based. The timespec data type is defined as a structure in the <time.h> header.

      So I assume this is using the system clock, so is affected by the date.

    6. idok commented on Jan 17, 2019

      idokmannequin
      MannequinAuthor

      thanks @pablogsal.

      looks like duplicate of bugs.python.org/issue23428

      It seems like i cant trust the threading wait function and need to use busy waiting.

    7. vstinner commented on Jan 17, 2019

      @vstinner
      Member

      Oops, I posted the following comment on the wrong issue... I wanted to post the following comment on this issue!

      I'm sorrry, I read the issue too quickly and misunderstood it. I guess that it's a duplicate of bpo-23428: "Use the monotonic clock for thread conditions on POSIX platforms". This issue is blocked the libc...

    8. vstinner commented on Jan 17, 2019

      @vstinner
      Member

      looks like duplicate of bugs.python.org/issue23428

      Yes. I close this issue as a duplicate of bpo-23428. Let's discuss the bug there!

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