GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Round up the NIO select timeout again to not busy spin (#18137) by DavideD · Pull Request #18138 · netty/netty

Conversation

@DavideD

@DavideD DavideD commented Oct 28, 2026 •
edited by franz1981
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing by 1_000_000, which rounds to the nearest millisecond, while the comment says it rounds up. If the next scheduled task is less than 0.5 ms away the timeout is 0, so select(...) calls selectNow() and returns. The task is not due yet, so the event loop runs another iteration and does the same again until the deadline is reached. An event loop that has I/O and a pending timer therefore polls the selector in a loop: an application with a timer that fires every ms, 4 event loops and 4000 requests/s used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since #9799, so the timeout is only 0 if the deadline is within 5 microseconds. The 4.2 NioIoHandler was introduced (#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead of spinning. Timers can fire up to 1 ms later than before, like in 4.1. The epoll, kqueue and io_uring transports use nanosecond timeouts and are not affected.

Assisted-by: Claude Opus 5.5 [email protected]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@DavideD @franz1981
Round up the NIO select timeout again to not busy spin (netty#18137)
c1c29b8
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
@franz1981
franz1981 force-pushed the 18137-4.2-timeout-rounding branch from f558604 to c1c29b8 Compare October 28, 2026 13:43
franz1981
franz1981 approved these changes Oct 28, 2026
View reviewed changes

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It trades precision (for submillis remainder) Vs CPU usage, but spinning for long is quite bad so I prefer it that way tbh. Let's see the CI

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@normanmaurer

normanmaurer commented Oct 28, 2026

Copy link
Copy Markdown
Member

@DavideD please sign our icla: https://netty.io. Let me know once done

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Oct 28, 2026

Copy link
Copy Markdown
Member

@DavideD can you also double check if the same problem exists in either kqueue, epoll or io_uring transport ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

good point @normanmaurer !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DavideD

DavideD commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

I think only NIO is affected, all the other transports seem to be using nanoseconds, instead of rounded milliseconds:

  • kqueue: KQueueIoHandler passes the seconds and nanoseconds straight to kevent()as a timespec.
  • io_uring: IoUringIoHandler writes the delay into the kernel timespec of an IORING_OP_TIMEOUT.
  • epoll: with epoll_pwait2 the timespec is passed as is. Without it, delays under epollWaitThreshold (10 ms by default) arm the timerfd with nanosecond precision. Only longer delays (or whole milliseconds, or over 1 s) use epoll_wait with a truncated millisecond timeout. That can wake the loop less than 1 ms early once, but the next wait then goes through the timerfd, so it doesn't loop.

So only NIO is affected, because Selector.select(long) takes milliseconds and a timeout of 0 means selectNow().

I signed the ICLA.

normanmaurer and franz1981 reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer added this to the 4.2.20.Final milestone Oct 28, 2026
@normanmaurer normanmaurer added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 28, 2026
@testlens-app

testlens-app Bot commented Oct 28, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: c1c29b8
▶️ Tests: 41516 executed
⚪️ Checks: 28/28 completed


Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981
franz1981 merged commit 06518dc into netty:4.2 Oct 28, 2026
29 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Thanks @DavideD ❤️

DavideD reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Oct 28, 2026
Auto-port 5.0: Round up the NIO select timeout again to not busy spin (#18137) #18141
Merged
@netty-project-bot

netty-project-bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #18141

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 28, 2026
normanmaurer pushed a commit that referenced this pull request Oct 28, 2026
@netty-project-bot @DavideD
Auto-port 5.0: Round up the NIO select timeout again to not busy spin (…
cc517fd
…#18137) (#18141)

Auto-port of #18138 to 5.0
Cherry-picked commit: 06518dc

---
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s used
about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since #9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and
are not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>

Co-authored-by: Davide D'Alto <[email protected]>
@lhotari lhotari mentioned this pull request Nov 2, 2026
[fix][sec] Upgrade Netty to 4.2.20 and netty-tcnative to 2.0.85 to address several CVEs and bugs apache/pulsar#27310
Merged
1 of 11 tasks
@Stwissel Stwissel mentioned this pull request Nov 25, 2026
NioIoHandler (4.2) calls Selector.wakeup() on every busy-loop iteration that receives an external task; 4.1's NioEventLoop did not #18558
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@franz1981 franz1981 franz1981 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.20.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@DavideD @normanmaurer @franz1981 @netty-project-bot
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.