Skip to content
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

gh-125997: ensure that time.sleep(0) is not delayed on non-Windows platforms #128274

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure that :func:`time.sleep(0) <time.sleep>` does not accumulate delays on
non-Windows platforms. Patch by Bénédikt Tran.
49 changes: 49 additions & 0 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ module time

/* Forward declarations */
static int pysleep(PyTime_t timeout);
#ifndef MS_WINDOWS
static int pysleep_zero_posix(void);
#endif


typedef struct {
Expand Down Expand Up @@ -2213,6 +2216,11 @@ static int
pysleep(PyTime_t timeout)
{
assert(timeout >= 0);
#ifndef MS_WINDOWS
if (timeout == 0) {
return pysleep_zero_posix();
}
picnixz marked this conversation as resolved.
Show resolved Hide resolved
#endif

#ifndef MS_WINDOWS
#ifdef HAVE_CLOCK_NANOSLEEP
Expand Down Expand Up @@ -2390,3 +2398,44 @@ pysleep(PyTime_t timeout)
return -1;
#endif
}


#ifndef MS_WINDOWS
// time.sleep(0) optimized implementation.
// On error, raise an exception and return -1.
// On success, return 0.
picnixz marked this conversation as resolved.
Show resolved Hide resolved
static int
pysleep_zero_posix(void)
picnixz marked this conversation as resolved.
Show resolved Hide resolved
{
static struct timeval zero = {0, 0};
picnixz marked this conversation as resolved.
Show resolved Hide resolved

PyTime_t deadline, monotonic;
if (PyTime_Monotonic(&monotonic) < 0) {
return -1;
}
deadline = monotonic;
do {
int ret, err;
Py_BEGIN_ALLOW_THREADS
ret = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &zero);
picnixz marked this conversation as resolved.
Show resolved Hide resolved
err = errno;
picnixz marked this conversation as resolved.
Show resolved Hide resolved
Py_END_ALLOW_THREADS
if (ret == 0) {
break;
}
if (err != EINTR) {
errno = err;
picnixz marked this conversation as resolved.
Show resolved Hide resolved
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
/* sleep was interrupted by SIGINT */
if (PyErr_CheckSignals()) {
return -1;
}
if (PyTime_Monotonic(&monotonic) < 0) {
return -1;
}
} while (monotonic == deadline);
return 0;
}
#endif
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Modules/pyexpat.c - error_info_of -
Modules/pyexpat.c - handler_info -
Modules/termios.c - termios_constants -
Modules/timemodule.c init_timezone YEAR -
Modules/timemodule.c pysleep_zero_posix zero -
Objects/bytearrayobject.c - _PyByteArray_empty_string -
Objects/complexobject.c - c_1 -
Objects/exceptions.c - static_exceptions -
Expand Down
Loading