-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Retry system calls failing with EINTR #7508
Description
Original bug ID: 7508
Reporter: goswin
Status: acknowledged (set by @xavierleroy on 2017-03-24T17:26:21Z)
Resolution: open
Priority: normal
Severity: feature
Category: standard library
Monitored by: @diml @yallop
Bug description
I want to push worward something python did recently:
https://www.python.org/dev/peps/pep-0475/
== Abstract ==
System call wrappers provided in the standard library should be retried automatically when they fail with EINTR , to relieve application code from the burden of doing so.
By system calls, we mean the functions exposed by the standard C library pertaining to I/O or handling of other system resources.
== Rationale ==
=== Interrupted system calls ===
On POSIX systems, signals are common. Code calling system calls must be prepared to handle them. Examples of signals:
The most common signal is SIGINT , the signal sent when CTRL+c is pressed. By default, Python raises a KeyboardInterrupt exception when this signal is received.
When running subprocesses, the SIGCHLD signal is sent when a child process exits.
Resizing the terminal sends the SIGWINCH signal to the applications running in the terminal.
Putting the application in background (ex: press CTRL-z and then type the bg command) sends the SIGCONT signal.
Writing a C signal handler is difficult: only "async-signal-safe" functions can be called (for example, printf() and malloc() are not async-signal safe), and there are issues with reentrancy. Therefore, when a signal is received by a process during the execution of a system call, the system call can fail with the EINTR error to give the program an opportunity to handle the signal without the restriction on signal-safe functions.
In ocaml a signal is caught and recorded in the GC and the next time a GC sequence point is hit the ocaml signal handler is called. Which I believe is eaxctly the same as in Python and we can go the same way to make writing signal safe code more easy. The Unix and Thread modules are directly affected although IO functions in Pervasives should be checked too.
Steps to reproduce
Try using the Graphics module under unix in combination with Unix.sleep. Graphics uses SIGIO to monitor the X connection and thereby aborts the Unix.sleep at random times.