Skip to content

Report pending SO_ERROR when non-blocking recv returns EAGAIN - #360

Merged
bjosv merged 1 commit into
valkey-io:mainfrom
quanyeyang:fix/net-read-so-error
Sep 25, 2026
Merged

bjosv merged 1 commit into
valkey-io:mainfrom
quanyeyang:fix/net-read-so-error

Conversation

@quanyeyang

Copy link
Copy Markdown
Member

Summary

When recv() returns EAGAIN on a non-blocking socket, a pending
SO_ERROR (sk_err) is left unconsumed. EPOLLERR is level-triggered,
so the async event loop can spin at 100% CPU while treating the
connection as healthy.

valkeyNetRead() now checks getsockopt(SO_ERROR) on that path and
returns VALKEY_ERR_IO if a socket error is pending, so callers
disconnect and reconnect. EINTR is unchanged (still retry). The
successful 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_ERRQUEUE drain
("Drain socket error queue on connect") and does not replace it.

Related: valkey-io/valkey#4766

Why this is a separate fix

tcp_poll() reports EPOLLERR if either sk_err != 0 or
sk_error_queue is non-empty.

Condition How to clear Where we handle it
ICMP / stale error queue during connect, SO_ERROR == 0 recvmsg(MSG_ERRQUEUE) Already in valkeyCheckConnectDone
Established connection, sk_err set, recv still EAGAIN getsockopt(SO_ERROR) This PR

Neither check supersedes the other.

Test plan

  • make with -Werror
  • Existing client_test (including connect error-queue cases) still pass
  • Review: no behavior change when SO_ERROR == 0 (genuine EAGAIN)

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]>
@quanyeyang

Copy link
Copy Markdown
Member Author

@michael-grunder @bjosv this is the established-connection SO_ERROR half, complementary to #353. Mind taking a look?

@bjosv bjosv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/net.c Resolved
@michael-grunder

Copy link
Copy Markdown
Collaborator

Fix looks fine to me. I don't love adding a syscall per EAGAIN but if it's a correctness issue then there isn't much of a choice.

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.

@quanyeyang

Copy link
Copy Markdown
Member Author

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.

@michael-grunder

michael-grunder commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Thank you for the detailed explanation!

LGTM I say merge it.

@bjosv
bjosv merged commit 3284afd into valkey-io:main Sep 25, 2026
53 checks passed
@quanyeyang
quanyeyang deleted the fix/net-read-so-error branch September 25, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants