Skip to content

Commit 0bb57bf

Browse files
committed
unix/modtime: sleep(): Automatically restart after receiving EINTR.
THis is required to deal well with signals, signals being the closest analogue of hardware interrupts for POSIX. This is also CPython 3.5 compliant behavior (PEP 475). The main problem implementing this is to figure out how much time was spent in waiting so far/how much is remaining. It's well-known fact that Linux updates select()'s timeout value when returning with EINTR to the remaining wait time. Here's what POSIX-based standards say about this: (http://pubs.opengroup.org/onlinepubs/9699919799/functions/pselect.html): "Upon successful completion, the select() function may modify the object pointed to by the timeout argument." I.e. it allows to modify timeout value, but doesn't say how exactly it is modified. And actually, it allows such modification only "upon successful completion", which returning with EINTR error hardly is. POSIX also allows to request automatic EINTR restart for system calls using sigaction call with SA_RESTART flag, but here's what the same document says about it: "If SA_RESTART has been set for the interrupting signal, it is implementation-defined whether the function restarts or returns with [EINTR]." In other words, POSIX doesn't leave room for both portable and efficient handling of this matter, so the code just allows to manually select Linux-compatible behavior with MICROPY_SELECT_REMAINING_TIME option, or otherwise will just raise OSError. When systems with non-Linux behavior are found, they can be handled separately.
1 parent 9d0d6d3 commit 0bb57bf

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

unix/modtime.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
*/
2626

2727
#include <unistd.h>
28+
#include <errno.h>
2829
#include <string.h>
2930
#include <time.h>
3031
#include <sys/time.h>
3132
#include <math.h>
3233

3334
#include "py/runtime.h"
3435
#include "py/smallint.h"
36+
#include "py/mphal.h"
3537

3638
#ifdef _WIN32
3739
void msec_sleep_tv(struct timeval *tv) {
@@ -113,8 +115,23 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
113115
double ipart;
114116
tv.tv_usec = round(modf(val, &ipart) * 1000000);
115117
tv.tv_sec = ipart;
116-
sleep_select(0, NULL, NULL, NULL, &tv);
118+
int res;
119+
while (1) {
120+
res = sleep_select(0, NULL, NULL, NULL, &tv);
121+
#if MICROPY_SELECT_REMAINING_TIME
122+
// TODO: This assumes Linux behavior of modifying tv to the remaining
123+
// time.
124+
if (res != -1 || errno != EINTR) {
125+
break;
126+
}
127+
//printf("select: EINTR: %ld:%ld\n", tv.tv_sec, tv.tv_usec);
128+
#else
129+
break;
130+
#endif
131+
}
132+
RAISE_ERRNO(res, errno);
117133
#else
134+
// TODO: Handle EINTR
118135
sleep(mp_obj_get_int(arg));
119136
#endif
120137
return mp_const_none;

unix/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ void mp_unix_mark_exec(void);
205205
#define MICROPY_PLAT_DEV_MEM (1)
206206
#endif
207207

208+
// Assume that select() call, interrupted with a signal, and erroring
209+
// with EINTR, updates remaining timeout value.
210+
#define MICROPY_SELECT_REMAINING_TIME (1)
211+
208212
#ifdef __ANDROID__
209213
#include <android/api-level.h>
210214
#if __ANDROID_API__ < 4

0 commit comments

Comments
 (0)