-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-125997: ensure that time.sleep(0)
is not delayed on non-Windows platforms
#128274
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
Changes from 1 commit
3754e9e
3c0c4e6
c18f15f
8d270f2
2996937
80de853
c7aa428
f15436e
68e2988
afc6186
f6a817f
fb9eb41
ecb9636
1ed9877
23b4740
40155ff
153b326
7e351c9
285c54a
434da31
ff06516
40d56f1
c4ce9cc
54b6dde
405e473
b803045
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
time.sleep(0)
path
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -73,7 +73,7 @@ module time | |||||
/* Forward declarations */ | ||||||
static int pysleep(PyTime_t timeout); | ||||||
#ifndef MS_WINDOWS | ||||||
static int pysleep_zero_posix(void); | ||||||
static int pysleep_zero_posix(void); // see gh-125997 | ||||||
#endif | ||||||
|
||||||
|
||||||
|
@@ -2218,7 +2218,7 @@ pysleep(PyTime_t timeout) | |||||
assert(timeout >= 0); | ||||||
assert(!PyErr_Occurred()); | ||||||
#ifndef MS_WINDOWS | ||||||
if (timeout == 0) { | ||||||
if (timeout == 0) { // gh-125997 | ||||||
return pysleep_zero_posix(); | ||||||
} | ||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#endif | ||||||
|
@@ -2408,35 +2408,25 @@ pysleep(PyTime_t timeout) | |||||
// | ||||||
// Rationale | ||||||
// --------- | ||||||
// time.sleep(0) accumulates delays if we use nanosleep() or clock_nanosleep(). | ||||||
// To avoid this pitfall, we may either use select(0, NULL, NULL, NULL, &zero) | ||||||
// or sched_yield(). The former is implementation-sensitive while the latter | ||||||
// would explicit relinquish the CPU but is more portable [1]. | ||||||
// | ||||||
// While select() is less portable due to various implementation details | ||||||
// it is slightly faster [2]. In addition, implicitly calling the kernel's | ||||||
// algorithm in time.sleep(0) may not be what non-Windows users expect [3]. | ||||||
// | ||||||
// Therefore, we opt for a solution based on select() instead of sched_yield(). | ||||||
// | ||||||
// [1] On Linux, calling sched_yield() causes the kernel's scheduling algorithm | ||||||
// to run as well and could be inefficient in terms of CPU consumption if | ||||||
// time.sleep(0) is successively called multiple times. | ||||||
// | ||||||
// [2] Experimentally, the CPU consumption of a sched_yield() solution is | ||||||
// similar to that one based on select(), albeit slightly slower. | ||||||
// | ||||||
// [3] sched_yield() is not recommended when resources needed by other | ||||||
// schedulable threads are still held by the caller (which may be | ||||||
// the case) and using it with with nondeterministic scheduling policies | ||||||
// such as SCHED_OTHER (which is the default) results in an unspecified | ||||||
// behaviour. | ||||||
// time.sleep(0) accumulates delays in the generic implementation, but we can | ||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// skip some calls to `PyTime_Monotonic()` and other checks when the timeout | ||||||
// is zero. For details, see https://github.com/python/cpython/pull/128274. | ||||||
static int | ||||||
pysleep_zero_posix(void) | ||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
assert(!PyErr_Occurred()); | ||||||
|
||||||
int ret; | ||||||
int ret, err; | ||||||
Py_BEGIN_ALLOW_THREADS | ||||||
#ifdef HAVE_CLOCK_NANOSLEEP | ||||||
struct timespec zero = {0, 0}; | ||||||
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &zero, NULL); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that TIMER_ABSTIME is appropriate here:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I first thought about:
Without this flag, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm leaving for a few days but I'm still struggling to convince myself between the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I was wrong. See #128274 (comment). |
||||||
err = ret; | ||||||
#elif defined(HAVE_NANOSLEEP) | ||||||
struct timespec zero = {0, 0}; | ||||||
ret = nanosleep(&zero, NULL); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth it to have 3 code paths for sleep(0)? Can't we always use select()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had a long discussion on this matter but long story short:
|
||||||
err = errno; | ||||||
#else | ||||||
// POSIX-compliant select(2) allows the 'timeout' parameter to | ||||||
// be modified but also mandates that the function should return | ||||||
// immediately if *both* structure's fields are zero (which is | ||||||
|
@@ -2447,13 +2437,15 @@ pysleep_zero_posix(void) | |||||
// this is also the case for zero timeouts), we prefer supplying | ||||||
// a fresh timeout everytime. | ||||||
struct timeval zero = {0, 0}; | ||||||
Py_BEGIN_ALLOW_THREADS | ||||||
ret = select(0, NULL, NULL, NULL, &zero); | ||||||
err = errno; | ||||||
#endif | ||||||
Py_END_ALLOW_THREADS | ||||||
if (ret == 0) { | ||||||
return 0; | ||||||
} | ||||||
if (errno != EINTR) { | ||||||
if (err != EINTR) { | ||||||
errno = err; | ||||||
PyErr_SetFromErrno(PyExc_OSError); | ||||||
return -1; | ||||||
} | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.