You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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=Noneclosed_at=<Date2014-09-17.22:17:19.598>created_at=<Date2014-07-23.02:10:01.628>labels= ['type-feature']
title='Use a monotonic clock to compute timeouts'updated_at=<Date2014-09-17.22:17:19.596>user='https://github.com/vstinner'
Currently, socket methods (ex: accept, recv, recvfrom, recvmsg, send, sendto, sendmsg), threading.Lock.acquire() and threading.RLock.acquire() use the system clock to compute their timeout. It's fine for the first call. But if the call is interrupted and the timeout need to be recomputed, it can wait too short or too long. Timeouts must use a monotonic clock, not the system clock. See the PEP-418 for more information.
Python modules were already patched to use the time.monotonic() function implemented in Python 3.3.
Attached patch fixes also functions which still use the system clock to compute timeouts.
A major change of the patch is that a monotonic clock is now require to use Python 3.5. Last time I checked, there was only one OS without monotonic clock: GNU Hurd. Hurd maintainers can patch Python 3.5 to fallback on the system clock until Hurd provides a monotonic clock.
Another important change is that Python now depends on the librt on Solaris and on Linux with glibc older than 2.17 (clock_gettime is now available directly in the libc since glibc 2.17).
pytimespec.patch removes *private* functions which were exposed in the stable ABI:
_PyTime_gettimeofday()
_PyTime_gettimeofday_info()
It also removes private macros:
_PyTime_ADD_SECONDS()
_PyTime_INTERVAL()
In pymonotonic.patch, I reused the same name but I didn't notice that they were part of the stable ABI. In pymonotonic-2.patch, I changed names so the dynamic loader will not load a module and the compiler will complain that symbols are missing.
On overflow, _PyTimeSpec_add_sec() and _PyTimeSpec_add_us() sets the timestamp to the maximum value instead of returning an undefined value. I prefer functions over macros because I want to return an error on overflow (even if it's not used).
_PyTimeSpec_interval_us() also returns the maximum value in case of overflow.
_PyTimeSpec_interval_sec() should maybe take a mandatory round parameter instead of always rounding up.
I added _PyTime_unit_t for _threadmodule.c because acquire_timed() has a PY_TIMEOUT_T type which can be a long long. Maybe acquire_timed() can return an error if the timeout is too large, or loop? I would prefer to avoid this custom _PyTime_unit_t type.
I should maybe add unit tests for _PyTimeSpec_add_() and _PyTimeSpec_interval_() functions, as I did for _PyLong_FromTime_t(), _PyTime_ObjectToTimeval(), etc.
pytimespec.patch become too large, I splitted this patch into a new issue: issue bpo-22117. If this issue is rejected, I will rewrite pymonotonic-2.patch to use the _PyTime_timeval structure.
Ok, I prepared Python for monotonic clock, I attached an updated patch. It is now much simpler.
pymonotonic-3.patch:
time.monotonic() is now always available
_PyTime_Init() ensures that the operating system provides a monotonic clock and that the clock works
Python 3.5 now requires a monotonic clock. All operating systems supported by Python 3.5 provides a monotonic clock. GNU Hurd doesn't, but it is not supported.
drop try/except ImportError around "from time import monotonic"
use a monotonic clock in _thread, gc and socket modules to compute elapsed time and timeouts
Thanks for the review Antoine. I pushed the new version pymonotonic-4.patch with a minor change: in debug mode, pymonotonic() also ensures that the clock never goes backward!
Activity
vstinner commentedon Jul 23, 2014
Currently, socket methods (ex: accept, recv, recvfrom, recvmsg, send, sendto, sendmsg), threading.Lock.acquire() and threading.RLock.acquire() use the system clock to compute their timeout. It's fine for the first call. But if the call is interrupted and the timeout need to be recomputed, it can wait too short or too long. Timeouts must use a monotonic clock, not the system clock. See the PEP-418 for more information.
Python modules were already patched to use the time.monotonic() function implemented in Python 3.3.
Attached patch fixes also functions which still use the system clock to compute timeouts.
A major change of the patch is that a monotonic clock is now require to use Python 3.5. Last time I checked, there was only one OS without monotonic clock: GNU Hurd. Hurd maintainers can patch Python 3.5 to fallback on the system clock until Hurd provides a monotonic clock.
Another important change is that Python now depends on the librt on Solaris and on Linux with glibc older than 2.17 (clock_gettime is now available directly in the libc since glibc 2.17).
vstinner commentedon Jul 23, 2014
On FreeBSD and OpenBSD, clock_gettime() is directly available in the libc.
vstinner commentedon Jul 31, 2014
Status of CLOCK_MONOTONIC in Hurd:
https://github.com/ArneBab/hurd-web/blob/master/open_issues/clock_gettime.mdwn
vstinner commentedon Jul 31, 2014
pymonotonic.patch is large and difficult to review. I prefer to split it into two parts:
pytimespec.patch: Change pytime.h to use _PyTimeSpec structure (nanosecond resolution) instead of _PyTime_timeval structure (microsecond resolution)
pymonotonic-2.patch: Add _PyTimeSpec_monotonic() and _PyTimeSpec_monotonic_info()
vstinner commentedon Jul 31, 2014
pytimespec.patch removes *private* functions which were exposed in the stable ABI:
It also removes private macros:
In pymonotonic.patch, I reused the same name but I didn't notice that they were part of the stable ABI. In pymonotonic-2.patch, I changed names so the dynamic loader will not load a module and the compiler will complain that symbols are missing.
Replaced/renamed functions:
I aslo added new functions:
On overflow, _PyTimeSpec_add_sec() and _PyTimeSpec_add_us() sets the timestamp to the maximum value instead of returning an undefined value. I prefer functions over macros because I want to return an error on overflow (even if it's not used).
_PyTimeSpec_interval_us() also returns the maximum value in case of overflow.
_PyTimeSpec_interval_sec() should maybe take a mandatory round parameter instead of always rounding up.
I added _PyTime_unit_t for _threadmodule.c because acquire_timed() has a PY_TIMEOUT_T type which can be a long long. Maybe acquire_timed() can return an error if the timeout is too large, or loop? I would prefer to avoid this custom _PyTime_unit_t type.
I should maybe add unit tests for _PyTimeSpec_add_() and _PyTimeSpec_interval_() functions, as I did for _PyLong_FromTime_t(), _PyTime_ObjectToTimeval(), etc.
vstinner commentedon Jul 31, 2014
Oh, pymonotonic-2.patch didn't get its "review" link because it depends on pytimespec.patch which is not merged yet.
vstinner commentedon Jul 31, 2014
pytimespec.patch become too large, I splitted this patch into a new issue: issue bpo-22117. If this issue is rejected, I will rewrite pymonotonic-2.patch to use the _PyTime_timeval structure.
vstinner commentedon Aug 27, 2014
To have an even smaller patch, I created the issue bpo-22287 just to add the dependency to the librt module in pytime.c.
python-dev commentedon Aug 29, 2014
New changeset 668e0bf30042 by Victor Stinner in branch 'default':
Issue bpo-22043: _PyTime_Init() now checks if the system clock works.
http://hg.python.org/cpython/rev/668e0bf30042
python-dev commentedon Aug 29, 2014
New changeset 76bc15c918b1 by Victor Stinner in branch 'default':
Issue bpo-22043: Simplify time.perf_counter() on Windows
http://hg.python.org/cpython/rev/76bc15c918b1
python-dev commentedon Aug 29, 2014
New changeset ab81b4cdc33c by Victor Stinner in branch 'default':
Issue bpo-22043: Oops, fix perf_counter() on UNIX if no monotonic clock is
http://hg.python.org/cpython/rev/ab81b4cdc33c
vstinner commentedon Aug 29, 2014
Ok, I prepared Python for monotonic clock, I attached an updated patch. It is now much simpler.
pymonotonic-3.patch:
time.monotonic() is now always available
_PyTime_Init() ensures that the operating system provides a monotonic clock and that the clock works
Python 3.5 now requires a monotonic clock. All operating systems supported by Python 3.5 provides a monotonic clock. GNU Hurd doesn't, but it is not supported.
drop try/except ImportError around "from time import monotonic"
use a monotonic clock in _thread, gc and socket modules to compute elapsed time and timeouts
vstinner commentedon Aug 31, 2014
pymonotonic-4.patch: Updated patch (version 4) to address Antoine Pitrou's comments on Rietveld.
python-dev commentedon Sep 2, 2014
New changeset 330bd57685fc by Victor Stinner in branch 'default':
Issue bpo-22043: Fix _PyTime_gettimeofday() if HAVE_GETTIMEOFDAY
http://hg.python.org/cpython/rev/330bd57685fc
python-dev commentedon Sep 2, 2014
New changeset b12857782041 by Victor Stinner in branch 'default':
Issue bpo-22043: time.monotonic() is now always available
http://hg.python.org/cpython/rev/b12857782041
python-dev commentedon Sep 3, 2014
New changeset 9deef14393d5 by Victor Stinner in branch 'default':
Issue bpo-22043: Fix pymonotonic(), use tv_usec=-1 as a marker to skip
http://hg.python.org/cpython/rev/9deef14393d5
vstinner commentedon Sep 3, 2014
Thanks for the review Antoine. I pushed the new version pymonotonic-4.patch with a minor change: in debug mode, pymonotonic() also ensures that the clock never goes backward!
vstinner commentedon Sep 17, 2014
Buildbots are happy, I close the issue.