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
/* ---------- TCP I/O ---------- *//* Wait until `fd` is ready for `events` (POLLIN for a read, POLLOUT for a write) before retrying a syscall that returned EAGAIN/EWOULDBLOCK. The accepted client fds are non-blocking, so a recv/send can would-block even after the scheduler's poll reported the fd ready (a transient kernel state, a partial drain, etc.); treating that as EOF/error wrongly tears down a live connection (#1500). Block on the single fd rather than busy-spinning, and recheck the term flag on the 1s timeout so a shutdown still breaks the wait. Returns 1 to retry the syscall, 0 to give up (term requested or poll error). */static int sp_net_wait_io(int fd, short events) { for (;;) { if (sp_net_term_flag) return 0; struct pollfd pf; pf.fd = fd; pf.events = events; pf.revents = 0; int pr = poll(&pf, 1, 1000); if (pr > 0) return 1; if (pr == 0) continue; /* timeout: recheck term, keep waiting */ if (errno == EINTR) continue; return 0; }}int sp_net_write_str(int fd, const char *s) { size_t len = strlen(s); size_t off = 0; ssize_t n = send(fd, s + off, len - off, 0); if (n <= 0) { if (n < 0 && errno == EINTR) continue; if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLOUT)) continue; return -1; } off += (size_t)n; ssize_t w = send(fd, data + off, total - off, 0); if (w <= 0) { if (w < 0 && errno == EINTR) continue; if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLOUT)) continue; return -1; } off += (size_t)w; n = recv(fd, sp_net_recv_buf, (size_t)maxlen, 0); if (n >= 0) break; if (errno == EINTR) continue; /* e.g. SIGCHLD in a prefork server */ /* Non-blocking fd would-block: wait for readability and retry rather than reporting an empty result the caller can't tell from EOF. */ if ((errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLIN)) continue; sp_net_bin_len = 0; sp_net_recv_buf[0] = '\0'; return sp_net_recv_buf; ssize_t n = recv(fd, sp_net_recv_all_buf + total, (size_t)(max_bytes - total), 0); if (n < 0) { if (errno == EINTR) continue; /* retry rather than truncate */ if ((errno == EAGAIN || errno == EWOULDBLOCK) && sp_net_wait_io(fd, POLLIN)) continue; break; } if (n == 0) break; /* clean EOF */