Skip to content

Fix high CPU usage on Linux due to epoll busy-loop#191

Open
lhoward wants to merge 1 commit intoswhitty:mainfrom
PADL:lhoward/186v2
Open

Fix high CPU usage on Linux due to epoll busy-loop#191
lhoward wants to merge 1 commit intoswhitty:mainfrom
PADL:lhoward/186v2

Conversation

@lhoward
Copy link
Contributor

@lhoward lhoward commented Mar 25, 2026

NB: these fixes were generated by AmpCode. They appear to fix the issue and also do not break tests.

Three fixes for 100% CPU spinning when HTTPServer is idle on Linux:

  1. Use edge-triggered (EPOLLET) mode for all sockets, not just the canary eventfd. Level-triggered epoll causes epoll_wait() to return immediately when the listening socket is readable, creating a busy loop.

  2. Change accept() to wait on .read events only instead of .connection ([.read, .write]). A listening socket is always writable (EPOLLOUT), so registering for write events causes immediate spurious wakeups. accept() only needs readability (a pending connection).

  3. Skip redundant epoll_ctl(EPOLL_CTL_MOD) calls when the event mask hasn't changed. EPOLL_CTL_MOD re-arms edge-triggered events and can cause spurious wakeups when the condition is already met.

Fixes: #186

Three fixes for 100% CPU spinning when HTTPServer is idle on Linux:

1. Use edge-triggered (EPOLLET) mode for all sockets, not just the
   canary eventfd. Level-triggered epoll causes epoll_wait() to return
   immediately when the listening socket is readable, creating a busy
   loop.

2. Change accept() to wait on .read events only instead of .connection
   ([.read, .write]). A listening socket is always writable (EPOLLOUT),
   so registering for write events causes immediate spurious wakeups.
   accept() only needs readability (a pending connection).

3. Skip redundant epoll_ctl(EPOLL_CTL_MOD) calls when the event mask
   hasn't changed. EPOLL_CTL_MOD re-arms edge-triggered events and can
   cause spurious wakeups when the condition is already met.

Fixes: swhitty#186
@codecov
Copy link

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.80%. Comparing base (a8358d1) to head (8b464ad).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #191   +/-   ##
=======================================
  Coverage   92.80%   92.80%           
=======================================
  Files          68       68           
  Lines        3448     3448           
=======================================
  Hits         3200     3200           
  Misses        248      248           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

high CPU usage in FlyingSocks

1 participant