Repository navigation
Drain Linux socket error queue after nonblocking TCP connect - #1375
Conversation
Linux can retain an ICMP error queued while a TCP socket is in SYN state even when the connection subsequently succeeds. The stale queue entry keeps EPOLLERR asserted while SO_ERROR is zero and recv() returns EAGAIN, causing event loops to spin without making progress. Drain MSG_ERRQUEUE after successful completion of library-managed nonblocking TCP connections. Preserve errno and leave blocking, Unix-socket, user-fd, and non-Linux behavior unchanged. Add a deterministic Linux regression using TX timestamping to reproduce error-queue occupancy with IP_RECVERR disabled and verify that EPOLLERR clears while the connection remains usable.
|
Additional upstream context: The need for this workaround is caused by a Linux kernel bug: IPv4 TCP sockets receive extended-error-queue entries during connection establishment even when hiredis has not enabled |
|
Wow what a great find! This sentinel 100% CPU issue has been a pain point for years but no one could ever track down why. Kernel bug sure would do it 😄 Since there not as of yet a way to gate it based on kernel the only prudent thing would be to merge the fix. Also iit's far less impactful to do this housekeeping on connect than doing it every I will play around the PR over the weekend and merge. @uglide This may solve a years long redis-sentinel 100% CPU bug. |
|
Should we also drain in the |
A queued extended error can keep POLLERR or EPOLLERR asserted while a nonblocking TCP connection is still in progress. Drain MSG_ERRQUEUE before returning the pending status so the event loop waits for real connect progress instead of spinning.
Blocking contexts also use a nonblocking socket while connecting and can receive error-queue wakeups before completion. Drain queued errors for all TCP contexts and re-poll connections that remain pending while preserving the original timeout deadline. Extend the regression to verify that blocking-mode connect completion clears EPOLLERR.
Some Linux environments, including QEMU user emulation, do not support SO_TIMESTAMPING. Skip the error-queue regression when setsockopt reports ENOPROTOOPT while preserving failures for every other error. Use the common cleanup path for both skipped and completed tests.
Good point. Added this in 055752d: pending nonblocking TCP connects now drain the error queue before returning completed == 0, preventing the queued error from repeatedly waking the event loop while the handshake continues. |
michael-grunder
left a comment
There was a problem hiding this comment.
LGTM. I'll give the Redis guys time to weigh in but don't see any blockers myself.
|
I see there is a subsequent fix we need too? Unless anyone has objections I'll merge this one and then go through the second PR. |
|
@swills Because we need to pick this fix to old version, can you pick this PR to redis? |
|
Thanks @swills for tracking this down. We hit this in production on Kubernetes (Sentinel on 7.0.5, 8.2.1 and 8.8.0) and confirmed on a stuck Sentinel that the spinning sockets had |
## Issue Sentinel can enter a busy loop when a stale Linux socket error queue entry remains after TCP connection succeeds. Related Issue: - #9956 - #15908 ## Change Port [hiredis#1375](redis/hiredis#1375) into `deps/hiredis`. Drain the Linux error queue when TCP connect is pending or completed, preserving `errno`. Blocking connections continue polling with the remaining original timeout. Add the upstream timestamping regression to verify that `EPOLLERR` clears and the connection still reads `PONG`. --------- Co-authored-by: Steve Wills <[email protected]>
Summary
POLLERR/EPOLLERRassertedProblem
Linux can queue an ICMP connection error while a TCP socket is still in SYN state, even when the connection subsequently succeeds. The observed error had
ee_errno == ECONNREFUSEDand remained insk_error_queueafter the socket became established.This does not rely on
IP_RECVERRbeing enabled. Linux queued the observed error during the TCP connection attempt, and the regression explicitly verifies thatIP_RECVERRis disabled.The stale queue entry keeps
POLLERR/EPOLLERRasserted whileSO_ERRORis zero and normalrecv()calls returnEAGAIN. A level-triggered event loop can therefore dispatch the socket continuously without making progress.Fix
During Linux TCP connection establishment, redisCheckConnectDone() now drains MSG_ERRQUEUE before returning either pending or successful completion. Blocking API connections are included because hiredis temporarily uses a nonblocking descriptor while connecting; if such a connection remains pending after an error-queue wakeup, redisContextWaitReady() re-polls using the remaining original timeout. Unix sockets, user-provided file descriptors, and non-Linux platforms retain their existing behavior.
The drain uses
MSG_DONTWAIT, retriesEINTR, stops when the queue is empty, and preserves the caller'serrno. It runs only for library-managed TCP connections. Unix sockets, user-provided file descriptors, and non-Linux platforms retain their existing behavior.Handling the queue at connect completion clears the stale condition before the async context is marked connected and begins normal command processing.
Testing
The Linux regression uses software TX timestamping to create the same
sk_error_queueoccupancy condition deterministically, without raw sockets or an ICMP timing race. It then disables timestamping and verifies that:IP_RECVERR,SO_TIMESTAMPING, andSO_ERRORare zerorecv()returnsEAGAINEPOLLERRremains asserted while the queue entry existsEPOLLERRPONGThe regression failed before the fix and passed afterward.
Related: redis/redis#9956
CI: https://github.com/swills/hiredis/actions/runs/35277083630
Note
Medium Risk
Changes nonblocking TCP connect completion on Linux, which async clients rely on via
redisCheckConnectDone, but scope is limited to Linux TCP and other platforms/paths are unchanged.Overview
Fixes a Linux-only async networking bug where a stale socket error queue entry can leave
EPOLLERR/POLLERRset after a nonblocking TCP connect succeeds, starving level-triggered event loops even thoughSO_ERRORis clear and reads returnEAGAIN.On Linux TCP connections,
redisCheckConnectDone()now drainsMSG_ERRQUEUEvia a newredisDrainSocketErrorQueue()helper (non-blocking,EINTR-safe, preserveserrno). Blocking connect waiting inredisContextWaitReady()was refactored to re-poll and re-check completion until the connect finishes or the timeout elapses, instead of treating a singlepollwakeup as final.Adds a Linux regression in
test.cthat uses software TX timestamping to populate the error queue deterministically, then asserts connect completion clearsEPOLLERRand the socket can still readPONG.Reviewed by Cursor Bugbot for commit 1083fa3. Bugbot is set up for automated code reviews on this repo. Configure here.