This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python threading event wait influenced by date change
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: threading.Condition.wait(timeout) should use a monotonic clock: use pthread_condattr_setclock(CLOCK_MONOTONIC)
View: 12822
Assigned To: Nosy List: ido k, pablogsal, vstinner
Priority: normal Keywords:

Created on 2019-01-15 17:54 by ido k, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
wrong_wait_behaviour.py ido k, 2019-01-15 17:54
Messages (7)
msg333718 - (view) Author: ido k (ido k) Date: 2019-01-15 17:54
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?
msg333719 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-15 17:57
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".
msg333720 - (view) Author: ido k (ido k) Date: 2019-01-15 18:03
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?
msg333796 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-01-16 22:15
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.
msg333811 - (view) Author: ido k (ido k) Date: 2019-01-17 01:33
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.
msg333842 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-17 10:05
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...
msg333843 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-17 10:06
> 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!
History
Date User Action Args
2022-04-11 14:59:10adminsetgithub: 79928
2021-10-01 08:49:34vstinnersetsuperseder: Use the monotonic clock for thread conditions on POSIX platforms -> threading.Condition.wait(timeout) should use a monotonic clock: use pthread_condattr_setclock(CLOCK_MONOTONIC)
2019-01-17 10:06:55vstinnersetstatus: open -> closed
superseder: Use the monotonic clock for thread conditions on POSIX platforms
messages: + msg333843

stage: resolved
2019-01-17 10:05:51vstinnersetmessages: + msg333842
2019-01-17 01:33:39ido ksetresolution: duplicate
messages: + msg333811
2019-01-16 22:15:33pablogsalsetnosy: + pablogsal
messages: + msg333796
2019-01-15 18:03:52ido ksetmessages: + msg333720
2019-01-15 17:57:17vstinnersetnosy: + vstinner
messages: + msg333719
2019-01-15 17:54:33ido kcreate