-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Use a monotonic clock to compute timeouts #66242
Comments
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). |
On FreeBSD and OpenBSD, clock_gettime() is directly available in the libc. |
Status of CLOCK_MONOTONIC in Hurd: |
pymonotonic.patch is large and difficult to review. I prefer to split it into two parts:
|
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. |
Oh, pymonotonic-2.patch didn't get its "review" link because it depends on pytimespec.patch which is not merged yet. |
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. |
To have an even smaller patch, I created the issue bpo-22287 just to add the dependency to the librt module in pytime.c. |
New changeset 668e0bf30042 by Victor Stinner in branch 'default': |
New changeset 76bc15c918b1 by Victor Stinner in branch 'default': |
New changeset ab81b4cdc33c by Victor Stinner in branch 'default': |
Ok, I prepared Python for monotonic clock, I attached an updated patch. It is now much simpler. pymonotonic-3.patch:
|
pymonotonic-4.patch: Updated patch (version 4) to address Antoine Pitrou's comments on Rietveld. |
New changeset 330bd57685fc by Victor Stinner in branch 'default': |
New changeset b12857782041 by Victor Stinner in branch 'default': |
New changeset 9deef14393d5 by Victor Stinner in branch 'default': |
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! |
Buildbots are happy, I close the issue. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: