Skip to content

Auto-port 5.0: Round up the NIO select timeout again to not busy spin (#18137) - #18141

Merged
normanmaurer merged 1 commit into
5.0from
auto-port-pr-18138-to-5.0
Oct 28, 2026
Merged

normanmaurer merged 1 commit into
5.0from
auto-port-pr-18138-to-5.0

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

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]

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]>
(cherry picked from commit 06518dc)

@utafrali utafrali left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Clean port of the busy-spin fix from 4.1. The rounding bias change from 500_000 to 995_000 ns, the matching overflow guard adjustment, and the package-private visibility for testing are all consistent, and the three new tests cover the rounding boundaries, the overflow cap, and the regression scenario (sub-millisecond deadline no longer causing a spin loop).

@testlens-app

testlens-app Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Build PR / macos-x86_64-java25-boringssl build

🏷️ Commit: f99ba27
▶️ Tests: 36307 executed
🟡 Checks: 12/13 completed

Rerun Controls

Click the checkbox to trigger a rerun:

🔲 Rerun jobs


Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit cc517fd into 5.0 Oct 28, 2026
13 of 14 checks passed
@normanmaurer
normanmaurer deleted the auto-port-pr-18138-to-5.0 branch October 28, 2026 17:15
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Oct 28, 2026
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.

4 participants