3 ms·
First, as to SIGPIPE, I just don't want even to generate it (and try hard not to), much less catch it -- so if I can then I set its disposition to SIG_IGN. Sec
by cryptonector 9y ago
First, as to SIGPIPE, I just don't want even to generate it (and try hard not to), much less catch it -- so if I can then I set its disposition to SIG_IGN.
Second, as to exit conditions, I always set a sig_atomic_t variable to indicate that an exit has been requested via a signal, and I check it in the event loop, so even if the pipe is full, the event loop will see this and cannot miss it.
Third, if need be I use multiple pipes, one per-signal. I've only ever done this for SIGCHLD and SIGUSR1/2 -- that is, I'll have a pipe for all the exit-request signals (e.g., SIGTERM), one for SIGCHLD (if the process creates children processes), and one for SIGUSR1 and/or SIGUSR2.
Fourth, the pollable signal notification interfaces have caveats. I don't even need to think about or know them as long as I have a reliable way to handle signals (which the self-pipe pattern is).
Fifth, signals are not reliable. Generally, signal events can be coalesced (yes, I know...) -- coalescing them in the self-pipe pattern by simply ignoring EAGAIN conditions is just as well (provided you leave evidence of the signal in a sig_atomic_t if you need that evidence).
Sixth, select()/poll() suck, a lot, but they are portable, and more so than pselect()/ppoll(). And if you're using select() or poll() then the self-pipe pattern works very well and yields portable code.
Of course, whenever possible I prefer to use kqueue, epoll, or Solaris event ports. Since I have to work with Linux (sadness) I have to work with epoll (much much sadness), and for all of epoll's warts, it's fantastically better than select()/poll(). Even so, I prefer to self-pipe than to use signalfd() or what have you. I've been using the self-pipe trick for well over a decade and it's never been a problem.
It's true that I don't usually implement language run-times where I need to handle synchronous signals such as SIGSEGV or SIGBUS in creative ways, and if I did I might well make use of longjump() and such in signal handlers -- I'd probably write an async-signal-safe stack walker even, and stack trace printer, and I've looked into that in the past and know it can be and has been done. I do maintain a run-time, jq's, but it's as a toy by comparison to, say, a JVM, and it does not need to play such games.
- quotemstr 9y agoWhy does this have to be so hard? You don't need to worry about EAGAIN or the pipe buffer or whatever. Here's a simple waitable semaphore that never fills the pipe. struct waitsem { pthread_mutex_t down_lock; int count; int fd_r; int fd_w; }; int waitsem_init(struct waitsem* ws) { memset(ws, 0, sizeof (*ws)); if (pthread_mutex_init(&ws->down_lock)) { return -1; } int pipefd[2]; // Instead use pipe2 and O_CLOEXEC if your OS supports it if (pipe(pipefd)) { posix_mutex_destroy(&ws->down_lock); return -1; } ws->fd_r = pipefd[0]; ws->fd_w = pipefd[1]; return 0; } // This function is async signal safe. void waitsem_up(struct waitsem* ws) { if (!atomic_fetch_add_explict(&ws->count, 1, memory_order_relaxed)) { // We transitioned 0->1, so it's our job to write a byte to the // pipe and wake up concurrent waiters. if (TEMP_FAILURE_RETRY(write(ws->fd_w, ws, 1)) != 1) { abort(); // POSIX prohibits failure. } } } // When ws->fd_r is readable, ws can probably be downed. Does not // block. (To perform a blocking down, wait on ws->fd_r.) Not async // signal safe. bool waitsem_try_down(struct waitsem* ws) { // N.B. We need ws->down_lock to bound the number of bytes in the // pipe. If we didn't take it, an arbitrary number of concurrent // waitsem_try_down callers could stall between decrementing // ws->counter and reading the pipe byte, each allowing an // interleaving waitsem_up to write a byte and, in theory, // eventually overflow the pipe buffer. You might think "well, // waitsem_up() would just block until one of the concurrent // waitsem_try_down functions got around to finishing its read", but // you'd be wrong, since waitsem_up() can run on a thread also // running waitsem_try_down and deadlock! With the mutex in place, // we can have at most two bytes in the pipe at one time, well below // the POSIX minimum PIPE_BUF of 512. // N.B. nothing in this function sleeps, so posix_mutex_lock doesn't // actually block for any meaningful time. Thus, this function // behaves as a non-blocking function even though read(2) can, in // general, sleep. if (posix_mutex_lock(&ws->down_lock)) { abort(); // POSIX prohibits failure } int old_counter = atomic_load_explicit(&ws->count, memory_order_relaxed); bool success = false; do { assert(old_counter >= 0); if (old_counter == 0) { goto out; } } while (!atomic_compare_exchange_weak_explicit(&ws->count, &old_counter, old_counter - 1, memory_order_relaxed, memory_order_relaxed)); if (old_counter == 1) { // We transitioned 1->0, so it's our job to read the byte out of // the pipe. We know the byte is in the pipe, so we don't actually // block here. char dummy; if (TEMP_FAILURE_RETRY(read(&ws->fd_r, &dummy, 1)) != 1) abort(); // POSIX prohibits failure. } success = true; out: if (posix_mutex_unlock(&ws->down_lock)) { abort(); // POSIX prohibits failure. } return success; } // Blocks until we can successfully down WS. void waitsem_down(struct waitsem* ws) { while (!waitsem_try_down(ws)) { struct pollfd pfd = {ws->fd_r, POLL_IN, 0}; poll(&pfd, 1, -1); } }
- cryptonector 9y agoThis is... way more complicated than just ignoring EAGAIN because the implied coalescing is fine anyways.