Repository navigation
Conversation
|
Do we know when linux will append to the extended error queue even if you don't opt-in? It seemed like the kernel bug was only during connect but perhaps I misread that. |
sorry fro later reply , the origin pr opend for so long i need some time to refresh my memory |
## 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 - [x] `make` with `-Werror` - [x] Existing `client_test` (including connect error-queue cases) still pass - [ ] Review: no behavior change when `SO_ERROR == 0` (genuine `EAGAIN`) Signed-off-by: quanyeyang <[email protected]>
|
Sorry for the delayed response. I have rethought this problem, I would like to share my updated thoughts and concerns regarding this PR. @sundb The original Redis PR https://github.com/redis/redis/pull/15107 has been merged. Please take a look at this as well. What the problem looks likeThe flow chart below is how I understand the busy-loop: sequenceDiagram
participant K as Linux kernel / socket
participant E as epoll / ae event loop
participant H as hiredis
participant S as Sentinel
Note over K,S: Original problem before the fix
loop EPOLLERR condition remains and the fd stays registered
E->>K: 1. Call epoll_wait()
K->>E: 2. Return the fd with EPOLLERR
E->>E: 3. Map EPOLLERR to AE_READABLE and AE_WRITABLE
E->>H: 4. Invoke the registered read callback
H->>K: 5. Call recv(fd, buf, len, 0)
K->>H: 6. Return -1 with EAGAIN or EWOULDBLOCK
H->>H: 7. redisNetRead returns 0 and redisBufferRead returns REDIS_OK
H->>E: 8. Finish the current read callback
E->>S: 9. No disconnect callback is triggered
Note over E,H: 10. The fd remains registered, so the next epoll_wait immediately returns EPOLLERR again
end
Note over E,S: Result: the read callback repeats and the event loop enters a CPU busy-loop
For this loop to happen, all of these need to be true at the same time:
Step 3 is in if (e->events & EPOLLERR) mask |= AE_WRITABLE|AE_READABLE;Step 6 and step 7 are in Lines 86 to 89 in 058ebcd So in this model, what we see is: Root cause analysisWhen does epoll return EPOLLERRIn Linux v6.4, the TCP poll code in smp_rmb();
if (READ_ONCE(sk->sk_err) ||
!skb_queue_empty_lockless(&sk->sk_error_queue))
mask |= EPOLLERR;
return mask;So for a normal IPv4 TCP socket, there are two states that set
Below I only look at the second case. What sk_err means
After reading more of the Linux TCP receive path, I found one sentence in my earlier analysis of redis/redis#15107 is probably not correct:
At least for the Linux v6.4 normal TCP receive path I checked, this seems not true. When there is no data to return, And hiredis If the receive queue already has data, Based on my current understanding of this normal Linux TCP path, My concern about the current PRCurrent PR calls case SO_ERROR:
v.val = -sock_error(sk);
if (v.val == 0)
v.val = xchg(&sk->sk_err_soft, 0);
break;So But I also understand this risk needs some specific conditions. Current understandingBased on the Linux v6.4 normal TCP path I checked, I now think my At the same time, #1376 reads So with the evidence now, I tend to close my own #1376, to avoid a change that doesn't have enough basis and may enlarge the error handling scope. Especially That said, I'm not very familiar with the Linux network stack, other kernel versions, or all the platforms hiredis supports, so I may have missed some important paths. |
Issue
A non-blocking socket can return
EWOULDBLOCKwhile an asynchronous error remains pending inSO_ERROR. The event loop continues reportingEPOLLERR, but hiredis returns0without consuming or reporting the error, causing an async busy-loop.Related issue in redis repo: redis/redis#9956
More detail can check the origin PR in redis repo: redis/redis#15107
Change
Check
SO_ERRORwhenrecv()returnsEWOULDBLOCKon a non-blocking socket. ReturnREDIS_ERR_IOwhengetsockopt()fails or reports a socket error, while preserving retry behavior when no socket error is pending. KeepEINTRhandling unchanged.Note
Medium Risk
Changes core non-blocking read error handling in hiredis; behavior is narrower and fixes a production CPU spin, but mis-handling SO_ERROR could affect connection teardown on edge cases.
Overview
Fixes a 100% CPU busy-loop in non-blocking event-driven clients (e.g. Redis Sentinel) when a peer becomes unreachable and the kernel reports an async socket error via EPOLLERR while
recv()only returns EWOULDBLOCK/EAGAIN.In
redisNetRead, whenrecvfails withEWOULDBLOCKon a non-blocking connection, the code now readsSO_ERRORwithgetsockoptbefore returning “try again later.” If a pending socket error exists, it setsREDIS_ERR_IOand returns -1 so the connection is torn down instead of spinning the event loop.EINTRis handled in a separate branch, unchanged in behavior (still retry with return 0).Reviewed by Cursor Bugbot for commit d6710d4. Bugbot is set up for automated code reviews on this repo. Configure here.