Skip to content

Commit

Permalink
Merge pull request #132 from hartwork/call-pselect-twice
Browse files Browse the repository at this point in the history
Call `pselect` twice to not starve signal handling (alternative to #130)
  • Loading branch information
hartwork authored Nov 29, 2023
2 parents ebfcd1c + e94cecd commit 6869c8b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion ttyplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,32 @@ void finish(int signum) {
sigint_pending = 1;
}

int pselect_without_signal_starvation(
int nfds,
fd_set * readfds,
fd_set * writefds,
fd_set * exceptfds,
const struct timespec * timeout,
const sigset_t * sigmask) {
// With high pressure on file descriptors (e.g. with "ttyplot < /dev/zero")
// a call to `pselect` could stall signal delivery for 20+ seconds on Linux.
// To avoid that situation, we first do a call to `pselect` that is dedicated
// to signal delivery and that only.
// (Related: https://stackoverflow.com/q/62315082)
const struct timespec zero_timeout = { .tv_sec = 0, .tv_nsec = 0 };

// First call, signal delivery only
const int select_ret = pselect(0, NULL, NULL, NULL, &zero_timeout, sigmask);

const bool signal_received = ((select_ret == -1) && (errno == EINTR));
if (signal_received) {
return select_ret;
}

// Second call
return pselect(nfds, readfds, writefds, exceptfds, timeout, sigmask);
}

void redraw_screen(const char * errstr) {
if (window_big_enough_to_draw()) {
paint_plot();
Expand Down Expand Up @@ -483,7 +509,7 @@ int main(int argc, char *argv[]) {
} else {
timeout.tv_nsec = 500 * 1000 * 1000; // <=500 milliseconds for a healthy clock display
}
const int select_ret = pselect(select_nfds, &read_fds, NULL, NULL, &timeout, &empty_sigset);
const int select_ret = pselect_without_signal_starvation(select_nfds, &read_fds, NULL, NULL, &timeout, &empty_sigset);

const bool signal_received = ((select_ret == -1) && (errno == EINTR));

Expand Down

0 comments on commit 6869c8b

Please sign in to comment.