Commit b1e4d476

b1e4d47607fdMerged

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

Description

sp_net: retry recv/send on EAGAIN, not just EINTR

The accepted client fds are non-blocking, so recv()/send() can return EAGAIN/EWOULDBLOCK (would-block) even after the scheduler's poll reported the fd ready -- a transient kernel state. The I/O helpers treated that identically to a hard failure: sp_net_recv_some returned an empty buffer the caller cannot distinguish from EOF (so a live keep-alive connection was torn down as if it closed -- the #1500 wedge), and the write helpers returned -1 (dropping the response on a momentarily full send buffer).

Retry on EAGAIN as well as EINTR at all four data syscalls (recv_some, recv_all, write_str, write_bytes). Rather than busy-spinning a non-blocking fd, wait for readiness with a single-fd poll (POLLIN for a read, POLLOUT for a write) and retry; the 1s poll timeout rechecks the term flag so a shutdown still breaks the wait. poll()'s own EINTR handling and the non-blocking accept_nb are unchanged.

Test 975 (gcc+clang, sp_net suite included) green; the tep blog fiber server rebuilds and serves correctly under a concurrent burst.

Refs #1500.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
b1e4d47607fd
Parent
c848a8133f0a

lib/sp_net.cmodifiedView file

@@ -207,6 +207,27 @@
207207
208208 /* ---------- TCP I/O ---------- */
209209
210+/* Wait until `fd` is ready for `events` (POLLIN for a read, POLLOUT for a
211+ write) before retrying a syscall that returned EAGAIN/EWOULDBLOCK. The
212+ accepted client fds are non-blocking, so a recv/send can would-block even
213+ after the scheduler's poll reported the fd ready (a transient kernel state,
214+ a partial drain, etc.); treating that as EOF/error wrongly tears down a live
215+ connection (#1500). Block on the single fd rather than busy-spinning, and
216+ recheck the term flag on the 1s timeout so a shutdown still breaks the wait.
217+ Returns 1 to retry the syscall, 0 to give up (term requested or poll error). */
218+static int sp_net_wait_io(int fd, short events) {
219+ for (;;) {
220+ if (sp_net_term_flag) return 0;
221+ struct pollfd pf;
222+ pf.fd = fd; pf.events = events; pf.revents = 0;
223+ int pr = poll(&pf, 1, 1000);
224+ if (pr > 0) return 1;
225+ if (pr == 0) continue; /* timeout: recheck term, keep waiting */
226+ if (errno == EINTR) continue;
227+ return 0;
228+ }
229+}
230+
210231 int sp_net_write_str(int fd, const char *s) {
211232 size_t len = strlen(s);
212233 size_t off = 0;
@@ -214,6 +235,7 @@
214235 ssize_t n = send(fd, s + off, len - off, 0);
215236 if (n <= 0) {
216237 if (n < 0 && errno == EINTR) continue;
238+ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLOUT)) continue;
217239 return -1;
218240 }
219241 off += (size_t)n;
@@ -228,6 +250,7 @@
228250 ssize_t w = send(fd, data + off, total - off, 0);
229251 if (w <= 0) {
230252 if (w < 0 && errno == EINTR) continue;
253+ if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLOUT)) continue;
231254 return -1;
232255 }
233256 off += (size_t)w;
@@ -249,6 +272,9 @@
249272 n = recv(fd, sp_net_recv_buf, (size_t)maxlen, 0);
250273 if (n >= 0) break;
251274 if (errno == EINTR) continue; /* e.g. SIGCHLD in a prefork server */
275+ /* Non-blocking fd would-block: wait for readability and retry rather
276+ than reporting an empty result the caller can't tell from EOF. */
277+ if ((errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLIN)) continue;
252278 sp_net_bin_len = 0;
253279 sp_net_recv_buf[0] = '\0';
254280 return sp_net_recv_buf;
@@ -267,6 +293,7 @@
267293 ssize_t n = recv(fd, sp_net_recv_all_buf + total, (size_t)(max_bytes - total), 0);
268294 if (n < 0) {
269295 if (errno == EINTR) continue; /* retry rather than truncate */
296+ if ((errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLIN)) continue;
270297 break;
271298 }
272299 if (n == 0) break; /* clean EOF */