Commit 889dfabb

889dfabb3104Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

sp_net: exit cleanly on SIGTERM instead of 143

The blocking-accept server could exit 143 (128+SIGTERM) on shutdown: SA_RESETHAND reset the term handler to the default action after the first signal, so a second SIGTERM killed the process; and a signal landing in the window between the term-flag check and the accept() call left accept blocked (the signal already consumed), hanging until a second signal was sent.

Drop SA_RESETHAND so repeated term signals keep hitting the (idempotent) handler, and add a self-pipe: the handler writes a byte that a poll() before accept() waits on alongside the listen socket. A signal now wakes the poll immediately and the loop returns -1 for a clean exit, with no check-then-block race. Verified a forked accept-loop server exits 0 (not killed) on SIGTERM; suite green.

Refs #1384.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
889dfabb3104
Parent
0bad861d7413

lib/sp_net.cmodifiedView file

@@ -68,20 +68,38 @@
6868 /* ---------- graceful shutdown ---------- */
6969
7070 static volatile sig_atomic_t sp_net_term_flag = 0;
71+/* Self-pipe: the term handler writes a byte here so a blocked accept (via
72+ * poll) wakes even when the signal lands in the check-then-accept window. */
73+static int sp_net_sigpipe[2] = {-1, -1};
7174
7275 static void sp_net_term_signal(int sig) {
7376 (void)sig;
7477 sp_net_term_flag = 1;
78+ if (sp_net_sigpipe[1] >= 0) {
79+ ssize_t w = write(sp_net_sigpipe[1], "x", 1); /* write() is async-signal-safe */
80+ (void)w;
81+ }
7582 }
7683
7784 int sp_net_install_term_handlers(void) {
85+ if (sp_net_sigpipe[0] < 0 && pipe(sp_net_sigpipe) == 0) {
86+ for (int i = 0; i < 2; i++) {
87+ int fl = fcntl(sp_net_sigpipe[i], F_GETFL, 0);
88+ if (fl >= 0) fcntl(sp_net_sigpipe[i], F_SETFL, fl | O_NONBLOCK);
89+ int fd = fcntl(sp_net_sigpipe[i], F_GETFD, 0);
90+ if (fd >= 0) fcntl(sp_net_sigpipe[i], F_SETFD, fd | FD_CLOEXEC);
91+ }
92+ }
7893 struct sigaction sa;
7994 memset(&sa, 0, sizeof(sa));
8095 sa.sa_handler = sp_net_term_signal;
8196 sigemptyset(&sa.sa_mask);
82- /* SA_RESETHAND: a second signal restores the default action so a
83- * non-cooperative second Ctrl-C kills immediately. */
84- sa.sa_flags = SA_RESETHAND;
97+ /* No SA_RESETHAND and no SA_RESTART: a repeated term signal must keep
98+ * hitting this handler (idempotent flag set) rather than the default
99+ * action -- the latter killed the process with 143 instead of letting
100+ * the accept loop exit cleanly. Without SA_RESTART, a signal during a
101+ * blocked accept/poll surfaces as EINTR. */
102+ sa.sa_flags = 0;
85103 sigaction(SIGTERM, &sa, NULL);
86104 sigaction(SIGINT, &sa, NULL);
87105 return 0;
@@ -125,11 +143,26 @@
125143 socklen_t clen = sizeof(caddr);
126144 int fd;
127145 for (;;) {
128- /* Honor a term signal that arrived between accepts (flag set
129- * with no syscall in flight to interrupt) -- check before
130- * blocking, not only on EINTR, or the next accept() blocks
131- * forever with the flag already set. */
132146 if (sp_net_term_flag) return -1;
147+ /* Wait for an incoming connection OR a term signal (which wakes the
148+ * self-pipe). Polling first closes the check-then-block race: a
149+ * signal that set the flag/pipe before we block makes poll return
150+ * immediately, so we never enter a blocking accept with the flag
151+ * already set. */
152+ struct pollfd pfds[2];
153+ pfds[0].fd = sfd; pfds[0].events = POLLIN; pfds[0].revents = 0;
154+ nfds_t npfd = 1;
155+ if (sp_net_sigpipe[0] >= 0) {
156+ pfds[1].fd = sp_net_sigpipe[0]; pfds[1].events = POLLIN; pfds[1].revents = 0;
157+ npfd = 2;
158+ }
159+ int pr = poll(pfds, npfd, -1);
160+ if (sp_net_term_flag) return -1;
161+ if (pr < 0) {
162+ if (errno == EINTR) continue;
163+ return -1;
164+ }
165+ if (!(pfds[0].revents & POLLIN)) continue; /* woken by the self-pipe alone */
133166 fd = accept(sfd, (struct sockaddr *)&caddr, &clen);
134167 if (fd >= 0) return fd;
135168 if (errno == EINTR) {