Interrupted System Call
A blocking system call was interrupted by a signal before it could complete. This is not a fatal error but requires the caller to retry the operation. Most high-level language runtimes handle EINTR automatically, but low-level C code must retry manually.
- 1A signal (e.g. SIGCHLD, SIGALRM, SIGUSR1) was delivered to the process while it was blocked in a syscall.
- 2The application uses signal handlers and performs blocking I/O simultaneously.
- 3The process was stopped and continued (SIGSTOP/SIGCONT) while in a blocking call.
A blocking read call is interrupted by a signal arriving at the same time.
// C: retry loop pattern
ssize_t safe_read(int fd, void *buf, size_t n) {
ssize_t r;
do { r = read(fd, buf, n); } while (r == -1 && errno == EINTR);
return r;
}expected output
read() returned -1, errno = EINTR (Interrupted system call)
Fix 1
Retry the call in a loop on EINTR
WHEN In C/C++ code performing blocking I/O with signal handlers installed
ssize_t r;
do {
r = read(fd, buf, sizeof(buf));
} while (r == -1 && errno == EINTR);Why this works
Looping on EINTR ensures the read completes even if interrupted multiple times by signals.
Fix 2
Use SA_RESTART flag when installing signal handlers
WHEN When you want syscalls to restart automatically after signal delivery
struct sigaction sa = { .sa_handler = my_handler, .sa_flags = SA_RESTART };
sigaction(SIGUSR1, &sa, NULL);Why this works
SA_RESTART causes the kernel to transparently restart the interrupted syscall after the signal handler returns.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev