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 — Introduce IoHandleEventLoopGroup / IoHandleEventLoop and its implemen… by normanmaurer · Pull Request #13991 · netty/netty

Conversation

@normanmaurer

@normanmaurer normanmaurer commented Apr 21, 2024

Copy link
Copy Markdown
Member

…tations MultiThreadIoHandleEventLoopGroup / SingleThreadIoHandleEventLoop

Motivation:

At the moment each transport has its own EventLoopGroup and EventLoop implementation. Most of these are final because they are tightly coubled with the specific Channel implementations. This is unfortunate for multiple reasons, as for example:

  • It's hard to instrument EventLoop* implementations as these are not extensible at all or even if they are the same logic needs to be added to multiple EventLoop* implementations.
  • Registration / Deregistration is limited to Channels which remove flexibility as for example some Operation Systems allowing to also handle other IO (for example file IO) with something like an EventLoop (one example would be io_uring).
  • A lot of code-duplication between different EventLoop* implementations exists as most share kind of the same logic of running non IO tasks.

Modifications:

  • Add the concept of IoHandleEventLoopGroup / IoHandleEventLoop and the corrosponding IoHandle, IoHandler and IoHandlerFactory. This allows to only have a custom implement for IO handling per transport while share the same code for execution of tasks, scheduled task and ultimatly IO.
  • Add the implementations MultiThreadIoHandleEventLoopGroup and SingleThreadIoHandleEventLoop that can be used by all (or most) transport implementations by having these just provide their own IoHandler / IoHandlerFactory implementations.

Result:

More code sharing and easier of customizing EventLoops.

Sorry, something went wrong.

Uh oh!

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

spencergibb, ktoso, He-Pin, violetagg, franz1981, sullis, and idelpivnitskiy reacted with thumbs up emoji He-Pin, rkapsi, franz1981, sullis, and idelpivnitskiy reacted with heart emoji
All reactions
  • 👍 7 reactions
  • ❤️ 5 reactions
@normanmaurer

normanmaurer commented Apr 21, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

I am opening this a draft mainly because I wanted to show and proof that we can back port some of the changes that we did in main without breaking too much things. I think even thought this PR introduce some API breaking it shouldn’t affect almost any user. To proof this I did not change how we create the different EventLoopGroups in our testsuite and everything still works.

So while the old way of constructing Eventloops still work the new way would be a bit different:

Old API:

new EpollEventLoop()

New API:

new MultiThreadIoHandleEventLoopGroup(EpollHandler.newFactory();

The benefit of the new way is that it is quite easy to instrument / doctorate an EventLoop/EventLoopGroup now. Basically you can just extend MultiThreadIoHandleEventLoopGroup / SingleThreadIoHandleEventLoop and add all the customization once while be able to reuse it with any transport implementation.
This includes things like:

  • able to get a count of registered channels / handles
  • understand how long the io processing vs task processing took.
  • how many channels / handles were processed per loop run
  • customize / decorate promises
  • …

Beside this IoHandleEventLoop can support not only Channel but also other subtypes of IoHandle. This would for example allow us to add file io support with io_uring and still use the same eventloop as for sockets etc. I am sure there are more things we could do by adding this kind of flexibility.

With all this said I still think the change is too dangerous for a new 4.1.x release. With this in mind I wondered if we might want to aim for releasing 4.2.0 with the following change:

  • This PR
  • Change the minimum java version to 8
  • Maybe a few more low risk changes.

I would love to hear what other project members and the community think about this ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 21, 2024

Copy link
Copy Markdown
Member Author

@bryce-anderson @idelpivnitskiy @chrisvest @franz1981 @vietj @yawkat @trustin WDYT ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 21, 2024

Copy link
Copy Markdown
Member Author

If interested I can also change all the creation code of EventLoopGroups in the testsuite and examples. I just wanted to not do this for now to show how small the impact for most people should be

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer
normanmaurer requested a review from chrisvest April 21, 2024 17:27
@normanmaurer
Introduce IoHandleEventLoopGroup / IoHandleEventLoop and its implemen…
e9c9852
…tations MultiThreadIoHandleEventLoopGroup / SingleThreadIoHandleEventLoop

Motivation:

At the moment each transport has its own EventLoopGroup and EventLoop implementation. Most of these are final because they are tightly coubled with the specific Channel implementations.
This is unfortunate for multiple reasons, as for example:

- It's hard to instrument EventLoop* implementations as these are not extensible at all or even if they are the same logic needs to be added to multiple EventLoop* implementations.
- Registration / Deregistration is limited to Channels which remove flexibility as for example some Operation Systems allowing to also handle other IO (for example file IO) with
  something like an EventLoop (one example would be io_uring).
- A lot of code-duplication between different EventLoop* implementations exists as most share kind of the same logic of running non IO tasks.

Modifications:

- Add the concept of IoHandleEventLoopGroup / IoHandleEventLoop and the corrosponding IoHandle, IoHandler and IoHandlerFactory. This allows to only have a custom implement for IO handling per transport while share the same code for execution of tasks, scheduled task and ultimatly IO.
- Add the implementations MultiThreadIoHandleEventLoopGroup and SingleThreadIoHandleEventLoop that can be used by all (or most) transport implementations by having these just provide their own IoHandler / IoHandlerFactory implementations.

Result:

More code sharing and easier of customizing EventLoops.
@normanmaurer
normanmaurer force-pushed the eventloop_rework branch from 2620422 to e9c9852 Compare April 21, 2024 17:37
@normanmaurer
normanmaurer requested review from bryce-anderson, ejona86, franz1981, idelpivnitskiy, trustin and violetagg April 21, 2024 17:45
normanmaurer added 4 commits April 21, 2024 15:01
@normanmaurer
remove duplication
856352f
@normanmaurer
Fix epoll
3ae97bd
@normanmaurer
Cleanup
fe12c64
@normanmaurer
Fix pom
69be2de
@He-Pin

He-Pin commented Apr 22, 2024

Copy link
Copy Markdown
Contributor

Do you plan to align with the design in Netty 5?

All reactions

Sorry, something went wrong.

Uh oh!

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

yawkat
yawkat approved these changes Apr 22, 2024
View reviewed changes
Comment thread transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java Resolved

Uh oh!

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

Comment thread transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java Outdated
for (EventExecutor e: this) {
((EpollEventLoop) e).setIoRatio(ioRatio);
}
// NOOP

@yawkat yawkat Apr 22, 2024

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.

Same here, need to document that this does nothing now

Sorry, something went wrong.

Uh oh!

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

normanmaurer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollHandler.java Resolved

Uh oh!

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

Comment thread transport/src/main/java/io/netty/channel/IoExecutionContext.java Resolved

Uh oh!

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

Comment thread transport/src/main/java/io/netty/channel/IoHandler.java Resolved

Uh oh!

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

Comment thread transport/src/main/java/io/netty/channel/nio/NioHandler.java Resolved

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

Will look into the build timeout later today

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

Do you plan to align with the design in Netty 5?

What design you talk about ? The ideas was to introduce the same concept with minimal real world breakage

All reactions

Sorry, something went wrong.

Uh oh!

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

@He-Pin

He-Pin commented Apr 22, 2024

Copy link
Copy Markdown
Contributor

Do you plan to align with the design in Netty 5?

What design you talk about ? The ideas was to introduce the same concept with minimal real world breakage

I mean split the ioHandler with the event loop, I will take some time to look into this.

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer
Fix test timeout
d315184
@normanmaurer

normanmaurer commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

Do you plan to align with the design in Netty 5?

What design you talk about ? The ideas was to introduce the same concept with minimal real world breakage

I mean split the ioHandler with the event loop, I will take some time to look into this.

That's exactly what this PR does ;)

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

Also I think if we would switch to java8 I could maybe do something more flexible to support different IoHandle in the future.

He-Pin and bryce-anderson 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.

@violetagg

violetagg commented Apr 22, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

@normanmaurer I don't know whether we should discuss this here in this PR but

With all this said I still think the change is too dangerous for a new 4.1.x release. With this in mind I wondered if we might
want to aim for releasing 4.2.0 with the following change:

This PR
Change the minimum java version to 8
Maybe a few more low risk changes.

Have you considered moving out of incubation some of the projects? (io_uring, QUIC, HTTP/3)?

All reactions

Sorry, something went wrong.

Uh oh!

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

normanmaurer
normanmaurer commented Apr 22, 2024
View reviewed changes
Comment thread transport/src/main/java/io/netty/channel/IoHandleEventLoop.java Resolved

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

@normanmaurer I don't know whether we should discuss this here in this PR but

With all this said I still think the change is too dangerous for a new 4.1.x release. With this in mind I wondered if we might
want to aim for releasing 4.2.0 with the following change:

This PR
Change the minimum java version to 8
Maybe a few more low risk changes.

Have you considered moving out of incubation some of the projects? (io_uring, QUIC, HTTP/3)?

Yes but let's focus on this one first :)

violetagg and He-Pin 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.

@He-Pin

He-Pin commented Apr 22, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

I would like to +1 for moving to Java 8 on Netty 4.2.x, as Netty 4.1.x has a very long history, With Java 8's default method, lambda completableFuture, etc., we can achieve more than it is now.

We can submit a PR and wait to see if people complain about moving to Java 8 on Netty 4.2.x

All reactions

Sorry, something went wrong.

Uh oh!

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

He-Pin
He-Pin reviewed Apr 22, 2024
View reviewed changes
Comment thread pom.xml Resolved

Uh oh!

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

@normanmaurer

normanmaurer commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

I would like to +1 for moving to Java 8 on Netty 4.2.x, as Netty 4.1.x has a very long history, With Java 8's default method, lambda completableFuture, etc., we can achieve more than it is now.

We can submit a PR and wait to see if people complain about moving to Java 8 on Netty 4.2.x

I think we should definitely try or even use 11. Let's discuss about this as a followup.

All reactions

Sorry, something went wrong.

Uh oh!

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

chrisvest
chrisvest reviewed Apr 28, 2024
View reviewed changes

@chrisvest chrisvest left a comment

Copy link
Copy Markdown
Member

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.

A few comments

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pom.xml Resolved

Uh oh!

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

Comment thread pom.xml Resolved

Uh oh!

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

Comment thread transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollChannelConfig.java Resolved

Uh oh!

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

Comment thread transport/src/main/java/io/netty/channel/IoHandler.java Resolved

Uh oh!

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

Comment thread transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java Resolved

Uh oh!

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

Comment thread transport/src/main/java/io/netty/channel/nio/NioIoHandler.java Resolved

Uh oh!

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

@normanmaurer
Address comments of chris
6aea38f
@normanmaurer
normanmaurer requested review from He-Pin and chrisvest April 29, 2024 11:01
chrisvest
chrisvest approved these changes Apr 29, 2024
View reviewed changes
@normanmaurer
normanmaurer merged commit 596a1f0 into 4.2 Apr 29, 2024
@normanmaurer
normanmaurer deleted the eventloop_rework branch April 29, 2024 18:11
@normanmaurer

normanmaurer commented Apr 29, 2024

Copy link
Copy Markdown
Member Author

Thanks a lot for all the reviews.... This is now merged :)

All 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.0.Final milestone Apr 29, 2024
@normanmaurer normanmaurer mentioned this pull request Apr 30, 2024
Replace use of old EventLoopGroup construction with new way of constructing it #14020
Merged
normanmaurer added a commit that referenced this pull request Apr 30, 2024
@normanmaurer
Replace use of old EventLoopGroup construction with new way of constr…
0dbbf9a
…ucting it

Motiviation:

#13991 introduced a new way of how EventLoopGroups should be constructed. We should update our code to use the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
normanmaurer added a commit that referenced this pull request Apr 30, 2024
@normanmaurer
Replace use of old EventLoopGroup construction with new way of constr…
82316e5
…ucting it

Motiviation:

#13991 introduced a new way of how EventLoopGroups should be constructed. We should update our code to use the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
normanmaurer added a commit that referenced this pull request May 2, 2024
@normanmaurer
Replace use of old EventLoopGroup construction with new way of const…
c390810
…ructing it (#14020)

Motiviation:

#13991 introduced a new way of how
EventLoopGroups should be constructed. We should update our code to use
the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
AlohaWorld pushed a commit to AlohaWorld/Netty.Official.Examples that referenced this pull request Mar 1, 2026
@normanmaurer
Replace use of old EventLoopGroup construction with new way of const…
4c5fb67
…ructing it (#14020)

Motiviation:

netty/netty#13991 introduced a new way of how
EventLoopGroups should be constructed. We should update our code to use
the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
@normanmaurer normanmaurer mentioned this pull request Sep 15, 2026
Restore configurable ioRatio-based task scheduling in SingleThreadIoEventLoop #17481
Merged
normanmaurer added a commit that referenced this pull request Sep 22, 2026
@normanmaurer
Restore configurable ioRatio-based task scheduling in SingleThreadIoE…
22e4967
…ventLoop (#17481)

Motivation:

The unification done in #13991/#14557 replaced the per-transport ioRatio
scheduling with a fixed maxTaskProcessingQuantumMs cap on task
processing
before returning to IO. Under sustained load the task queue is never
empty, so the event loop can spend up to the full quantum on tasks every
iteration regardless of how little time was just spent on IO, delaying
IO servicing (and so response completion) for a minority of requests.
This was observed as an elevated mean (but not median) round-trip
latency after upgrading from 4.1 to 4.2, which went away on rollback.

Modifications:

- Add back a volatile ioRatio field and functional getIoRatio()/
  setIoRatio(int) on SingleThreadIoEventLoop, configurable via the new
  io.netty.eventLoop.ioRatio system property (default 100, which
  preserves the current fixed-quantum behaviour unchanged).
- When ioRatio < 100, size each iteration's task budget as
  ioTime * (100 - ioRatio) / ioRatio, capped by
  maxTaskProcessingQuantumNs, matching the pre-4.2 NioEventLoop formula
  while keeping the existing quantum as a safety upper bound.
- Add setter / getter to MultiThreadIoEventLoopGroup so it is supported
globally

Result:

Users hitting the 4.2 scheduling regression can restore proportional
IO/task balancing with a single setIoRatio(50) call (or the new system
property), with no change in behaviour for anyone who doesn't opt in.
@normanmaurer normanmaurer mentioned this pull request Sep 23, 2026
Port: Restore configurable ioRatio-based task scheduling in SingleThreadIoEventLoop (#17481) #17603
Merged
normanmaurer added a commit that referenced this pull request Sep 25, 2026
@normanmaurer
Port: Restore configurable ioRatio-based task scheduling in SingleThr…
5e9389c
…eadIoEventLoop (#17481) (#17603)

Motivation:

The unification done in #13991/#14557 replaced the per-transport ioRatio
scheduling with a fixed maxTaskProcessingQuantumMs cap on task
processing
before returning to IO. Under sustained load the task queue is never
empty, so the event loop can spend up to the full quantum on tasks every
iteration regardless of how little time was just spent on IO, delaying
IO servicing (and so response completion) for a minority of requests.
This was observed as an elevated mean (but not median) round-trip
latency after upgrading from 4.1 to 4.2, which went away on rollback.

Modifications:

- Add back a volatile ioRatio field and functional getIoRatio()/
  setIoRatio(int) on SingleThreadIoEventLoop, configurable via the new
  io.netty.eventLoop.ioRatio system property (default 100, which
  preserves the current fixed-quantum behaviour unchanged).
- When ioRatio < 100, size each iteration's task budget as
  ioTime * (100 - ioRatio) / ioRatio, capped by
  maxTaskProcessingQuantumNs, matching the pre-4.2 NioEventLoop formula
  while keeping the existing quantum as a safety upper bound.
- Add setter / getter to MultiThreadIoEventLoopGroup so it is supported
globally

Result:

Users hitting the 4.2 scheduling regression can restore proportional
IO/task balancing with a single setIoRatio(50) call (or the new system
property), with no change in behaviour for anyone who doesn't opt in.
@netudima netudima mentioned this pull request Oct 27, 2026
[CASSANDRA-21175][trunk] Upgrade Netty to 4.2.18.Final and netty-tcnative to 2.0.84.Final apache/cassandra#5076
Open
@DavideD DavideD mentioned this pull request Oct 28, 2026
Vert.x 5 on NIO: event loops busy-poll the selector when timers are pending (Netty 4.2), about 3x CPU per request #18137
Closed
DavideD added a commit to DavideD/netty that referenced this pull request Oct 28, 2026
@DavideD
Round up the NIO select timeout again to not busy spin (netty#18137)
f558604
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]>
@DavideD DavideD mentioned this pull request Oct 28, 2026
Round up the NIO select timeout again to not busy spin (#18137) #18138
Merged
franz1981 pushed a commit to DavideD/netty that referenced this pull request Oct 28, 2026
@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 pushed a commit that referenced this pull request Oct 28, 2026
@DavideD @franz1981
Round up the NIO select timeout again to not busy spin (#18137)
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]>
@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
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]>
@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

@yawkat yawkat yawkat approved these changes
@bryce-anderson bryce-anderson bryce-anderson approved these changes
@chrisvest chrisvest chrisvest approved these changes
@trustin trustin Awaiting requested review from trustin
@violetagg violetagg Awaiting requested review from violetagg
@ejona86 ejona86 Awaiting requested review from ejona86
@idelpivnitskiy idelpivnitskiy Awaiting requested review from idelpivnitskiy
@franz1981 franz1981 Awaiting requested review from franz1981
@He-Pin He-Pin Awaiting requested review from He-Pin

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.0.Alpha1

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

6 participants

@normanmaurer @He-Pin @violetagg @chrisvest @yawkat @bryce-anderson
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.