Repository navigation
Report pending SO_ERROR when non-blocking recv returns EAGAIN - #360
Conversation
recv() does not consume sk_err, so EPOLLERR stays level-triggered and the async event loop can spin at 100% CPU. Surface the error so the connection is torn down. Signed-off-by: quanyeyang <[email protected]>
|
@michael-grunder @bjosv this is the established-connection SO_ERROR half, complementary to #353. Mind taking a look? |
|
Fix looks fine to me. I don't love adding a syscall per I'm a bit curious as to how the edge case occurs. It must be rare or libvalkey and hiredis before it would've been a 100% CPU spin machine. |
The busy-loop needs a few things at once: the fd already looks connected, a pending async error is sitting on the socket so tcp_poll keeps reporting level-triggered EPOLLERR, the receive queue is empty so recv still returns EAGAIN, and the event loop maps EPOLLERR to the read handler and treats that EAGAIN as “still healthy”. The usual close paths never hit this. Peer FIN is recv == 0, hard RST is recv == -ECONNRESET, and connect-time failures are already handled by SO_ERROR / MSG_ERRQUEUE. What remains is an error recv is not required to surface — that is exactly what SO_ERROR is for on non-blocking sockets. That is why this showed up in Sentinel at boot (valkey#4766) rather than in every hiredis client: many connections while routing or peers are not up yet, plus ae’s LT epoll treating ERR as readable. I agree the extra getsockopt on a genuine EAGAIN is not free. We only fail when SO_ERROR != 0, and the alternative is leaving EPOLLERR latched. libvalkey’s read callback also does not see the epoll mask, so the check has to live here. |
|
Thank you for the detailed explanation! LGTM I say merge it. |
Summary
When
recv()returnsEAGAINon a non-blocking socket, a pendingSO_ERROR(sk_err) is left unconsumed.EPOLLERRis level-triggered,so the async event loop can spin at 100% CPU while treating the
connection as healthy.
valkeyNetRead()now checksgetsockopt(SO_ERROR)on that path andreturns
VALKEY_ERR_IOif a socket error is pending, so callersdisconnect and reconnect.
EINTRis unchanged (still retry). Thesuccessful
recv()path is unchanged.This is the same fix as redis/redis#15107 and redis/hiredis#1376.
It complements the already-merged connect-time
MSG_ERRQUEUEdrain("Drain socket error queue on connect") and does not replace it.
Related: valkey-io/valkey#4766
Why this is a separate fix
tcp_poll()reportsEPOLLERRif eithersk_err != 0orsk_error_queueis non-empty.SO_ERROR == 0recvmsg(MSG_ERRQUEUE)valkeyCheckConnectDonesk_errset,recvstillEAGAINgetsockopt(SO_ERROR)Neither check supersedes the other.
Test plan
makewith-Werrorclient_test(including connect error-queue cases) still passSO_ERROR == 0(genuineEAGAIN)