Socket: treat read()/recv()==0 as EOF before consulting errno#224
Open
lhoward wants to merge 2 commits into
Open
Socket: treat read()/recv()==0 as EOF before consulting errno#224lhoward wants to merge 2 commits into
lhoward wants to merge 2 commits into
Conversation
read()/recvfrom()/recvmsg() returning 0 is an orderly EOF, not an error, and does not set errno. The error classification checked errno == EWOULDBLOCK before count == 0, so an EOF read whose errno was left as EWOULDBLOCK by an earlier would-block read on the same thread was misclassified as .blocked instead of .disconnected. The reader then re-suspended waiting for more data instead of closing. Under concurrency (errno is per-thread) this both leaked the connection (CLOSE-WAIT, never closed) and, because epoll re-reports the readable EOF socket, spun the pool re-arming and re-reading it, pegging CPU. Check count == 0 first; errno is only meaningful after a -1 return.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #224 +/- ##
==========================================
- Coverage 92.92% 92.91% -0.01%
==========================================
Files 71 71
Lines 3719 3728 +9
==========================================
+ Hits 3456 3464 +8
- Misses 263 264 +1 ☔ View full report in Codecov by Harness. |
Contributor
Author
|
FWIW this appears to be the final fix in my Claude-assisted to quest to eliminate FlyingFox-related CPU usage spikes and FD leaks on Linux. |
lhoward
added a commit
to PADL/OpenSRP
that referenced
this pull request
Jul 14, 2026
epoll_wait returns -1/EINTR when a signal interrupts it, which is not a
fatal condition. getNotifications() treated any non-positive return as a
failure and threw SocketError.makeFailed("epoll wait"), tearing down the
server ("epoll wait(4): Interrupted system call"). Return no events on
EINTR so the caller polls again.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Kkgk9vUQAZUTMUnbus1RR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
read()/recvfrom()/recvmsg()return of0is an orderly EOF, not an error, and does not seterrno. The error-classificationguardcheckederrno == EWOULDBLOCKbeforecount == 0:So an EOF read whose
errnowas left asEWOULDBLOCKby an earlier would-block read on the same thread is misclassified as.blockedinstead of.disconnected. The reader then re-suspends waiting for more data instead of closing.Because
errnois per-thread, this only manifests under concurrency (a would-block read on one connection contaminating an EOF read on another). The consequences:CLOSE-WAIT(fd leak).epollkeeps re-reporting the readable EOF socket, so the pool spins re-arming (EPOLL_CTL_ADD/DEL) and re-reading it (read()==0), pegging CPU.Reproducible with a handful of concurrent half-closed keep-alive connections (
shutdown(SHUT_WR), held open): the server pins a core and accumulatesCLOSE-WAITsockets. With the fix, CPU stays at 0 and no sockets leak.Fix: check
count == 0first;errnois only meaningful after a-1return. Applied toread,recvFrom, andrecvMsg.