tenox7 / ttyplot

a realtime plotting utility for terminal/console with data input from stdin
Apache License 2.0
956 stars 42 forks source link

Catch signals on Linux with signalfd (follow-up to #98 and #132) #142

Closed edgar-bonet closed 7 months ago

edgar-bonet commented 7 months ago

As noticed by @hartwork, pselect is not a reliable way of catching signals on Linux: in case of high input pressure, signal delivery is starved. Pull request #132 partially fixed the issue by issuing two consecutive pselect calls, but this left a race window between these two calls.

This pull request fixes the issue without race conditions by using signalfd(), which makes signal delivery reliable. The call is only issued on Linux, as both the pselect() issue and signalfd() are Linux-specific.

This is one of two possible alternatives, the other one being the self-pipe trick (#143).

hartwork commented 7 months ago

@edgar-bonet PS: nice demo, by the way :+1:

edgar-bonet commented 7 months ago

@hartwork: This approach is indeed problematic from the point of view of testing. One trick I used is to #undef __linux__ at the top, which lets me exercise the BSD code paths on Linux: as expected, it works, except for signals being missed under high stdin pressure.

I do also prefer the self-pipe approach: less code, and conceptually simpler as all events (signals, keystrokes and data) are caught in the exact same way: as “file descriptor ready” events. Closing this PR. I will work on #143, later, on the basis of your review.