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 — Epoll: Fix excessive CPU usage when Channel is only registered but no… · netty/netty@fce9414

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-classes-epoll/src/main/java/io/netty/channel/epoll
    • AbstractEpollChannel.java
    • EpollDatagramChannel.java
    • EpollIoHandler.java
    • EpollServerSocketChannel.java
  • transport-native-epoll/src/test/java/io/netty/channel/epoll
    • EpollIoHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@
6060

6161
abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel {
6262
protected final LinuxSocket socket;
63+
private final EpollIoOps inital;
6364
private final EpollIoHandleImpl ioHandle = new EpollIoHandleImpl();
6465
private Promise<Void> connectPromise;
66+
6567
private SocketAddress requestedRemoteAddress;
6668
private volatile SocketAddress local;
6769
private volatile SocketAddress remote;
6870

6971
private IoRegistration registration;
7072
boolean inputClosedSeenErrorOnRead;
7173
private EpollIoOps ops;
72-
private EpollIoOps inital;
7374

7475
protected volatile boolean active;
7576

@@ -84,6 +85,7 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
8485
this.local = fd.localAddress();
8586
this.remote = fd.remoteAddress();
8687
}
88+
this.inital = initialOps;
8789
this.ops = initialOps;
8890
}
8991

@@ -96,6 +98,7 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
9698
// See https://github.com/netty/netty/issues/2359
9799
this.remote = remote;
98100
this.local = fd.localAddress();
101+
this.inital = initialOps;
99102
this.ops = initialOps;
100103
}
101104

@@ -279,8 +282,10 @@ protected void doRegister(Promise<Void> promise) {
279282
executor().register(ioHandle).addListener(f -> {
280283
if (f.isSuccess()) {
281284
registration = (IoRegistration) f.getNow();
282-
registration.submit(ops);
283-
inital = ops;
285+
if (isActive()) {
286+
// The channel is active, register with current ops now as we are ready to start receiving events.
287+
submitCurrentOps();
288+
}
284289
promise.setSuccess(null);
285290
} else {
286291
promise.setFailure(f.cause());
@@ -655,6 +660,9 @@ private boolean doFinishConnect() throws Exception {
655660
requestedRemoteAddress = null;
656661
active = true;
657662

663+
// The channel is active, register with current ops now as we are ready to start receiving events.
664+
submitCurrentOps();
665+
658666
return true;
659667
}
660668
setFlag(Native.EPOLLOUT);
@@ -706,6 +714,8 @@ protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddr
706714
remote = remoteSocketAddr == null ?
707715
remoteAddress : computeRemoteAddr(remoteSocketAddr, (InetSocketAddress) socket.remoteAddress());
708716
active = true;
717+
// The channel is active, register with current ops now as we are ready to start receiving events.
718+
submitCurrentOps();
709719
}
710720
// We always need to set the localAddress even if not connected yet as the bind already took place.
711721
//
@@ -730,6 +740,11 @@ boolean doConnect0(SocketAddress remote) throws Exception {
730740
}
731741
}
732742

743+
final void submitCurrentOps() {
744+
IoRegistration registration = registration();
745+
registration.submit(ops);
746+
}
747+
733748
@Override
734749
protected SocketAddress localAddress0() {
735750
return local;
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ public void block(
305305
}
306306
}
307307

308+
@Override
309+
protected void doRegister(Promise<Void> promise) {
310+
super.doRegister(promise);
311+
promise.addListener(f -> {
312+
if (f.isSuccess() && isRegistered()) {
313+
// As Datagram is connection-less we can submit the current ops once the registration itself was
314+
// successful.
315+
submitCurrentOps();
316+
}
317+
});
318+
}
319+
308320
@Override
309321
protected void doBind(SocketAddress localAddress, Promise<Void> promise) {
310322
if (localAddress instanceof InetSocketAddress) {
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollIoHandler.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollIoHandler.java
+32-13Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838

3939
import java.io.IOException;
4040
import java.io.UncheckedIOException;
41-
import java.util.concurrent.atomic.AtomicBoolean;
4241
import java.util.concurrent.atomic.AtomicLong;
4342

4443
import static java.lang.Math.min;
45-
import static java.lang.System.nanoTime;
4644

4745
/**
4846
* {@link IoHandler} which uses epoll under the covers. Only works on Linux!
@@ -222,9 +220,19 @@ public void destroy() {
222220
}
223221
}
224222

225-
private final class DefaultEpollIoRegistration implements IoRegistration {
223+
private enum RegistrationState {
224+
// Was not added via EPOLL_CTL_ADD
225+
Pending,
226+
// Was added via EPOLL_CTL_ADD
227+
Added,
228+
// Was canceled an so removed via EPOLL_CTL_DEL
229+
Cancelled
230+
}
231+
232+
private final class DefaultEpollIoRegistration
233+
implements IoRegistration {
226234
private final ThreadAwareExecutor executor;
227-
private final AtomicBoolean canceled = new AtomicBoolean();
235+
private RegistrationState state = RegistrationState.Pending;
228236
final EpollIoHandle handle;
229237

230238
DefaultEpollIoRegistration(ThreadAwareExecutor executor, EpollIoHandle handle) {
@@ -242,25 +250,37 @@ public <T> T attachment() {
242250
public long submit(IoOps ops) {
243251
EpollIoOps epollIoOps = cast(ops);
244252
try {
245-
if (!isValid()) {
246-
return -1;
253+
synchronized (this) {
254+
switch (state) {
255+
case Cancelled:
256+
return -1;
257+
case Pending:
258+
Native.epollCtlAdd(epollFd.intValue(), handle.fd().intValue(), epollIoOps.value);
259+
state = RegistrationState.Added;
260+
case Added:
261+
Native.epollCtlMod(epollFd.intValue(), handle.fd().intValue(), epollIoOps.value);
262+
return epollIoOps.value;
263+
default:
264+
throw new IllegalStateException();
265+
}
247266
}
248-
Native.epollCtlMod(epollFd.intValue(), handle.fd().intValue(), epollIoOps.value);
249-
return epollIoOps.value;
250267
} catch (IOException e) {
251268
throw new UncheckedIOException(e);
252269
}
253270
}
254271

255272
@Override
256-
public boolean isValid() {
257-
return !canceled.get();
273+
public synchronized boolean isValid() {
274+
return state != RegistrationState.Cancelled;
258275
}
259276

260277
@Override
261278
public boolean cancel() {
262-
if (!canceled.compareAndSet(false, true)) {
263-
return false;
279+
synchronized (this) {
280+
if (state == RegistrationState.Cancelled) {
281+
return false;
282+
}
283+
state = RegistrationState.Cancelled;
264284
}
265285
if (executor.isExecutorThread(Thread.currentThread())) {
266286
cancel0();
@@ -316,7 +336,6 @@ public IoRegistration register(IoHandle handle)
316336
final EpollIoHandle epollHandle = cast(handle);
317337
DefaultEpollIoRegistration registration = new DefaultEpollIoRegistration(executor, epollHandle);
318338
int fd = epollHandle.fd().intValue();
319-
Native.epollCtlAdd(epollFd.intValue(), fd, EpollIoOps.EPOLLERR.value);
320339
DefaultEpollIoRegistration old = registrations.put(fd, registration);
321340

322341
// We either expect to have no registration in the map with the same FD or that the FD of the old registration
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollServerSocketChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollServerSocketChannel.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ protected void doBind(SocketAddress localAddress, Promise<Void> promise) {
183183
}
184184
socket.listen(config.getBacklog());
185185
active = true;
186+
187+
// We now listen for new connections, submit the ops so we receive events.
188+
submitCurrentOps();
186189
} catch (Throwable cause) {
187190
promise.setFailure(cause);
188191
return;
Collapse file

‎transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollIoHandlerTest.java‎

Copy file name to clipboard
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.channel.epoll;
17+
18+
import io.netty.channel.IoEvent;
19+
import io.netty.channel.IoHandler;
20+
import io.netty.channel.IoHandlerContext;
21+
import io.netty.channel.IoHandlerFactory;
22+
import io.netty.channel.IoRegistration;
23+
import io.netty.channel.unix.FileDescriptor;
24+
import io.netty.util.concurrent.ThreadAwareExecutor;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.concurrent.atomic.AtomicReference;
28+
29+
import static org.junit.jupiter.api.Assertions.assertNull;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
public class EpollIoHandlerTest {
33+
@Test
34+
public void testRegisterWillNotTriggerEvent() throws Exception {
35+
IoHandlerFactory ioHandlerFactory = EpollIoHandler.newFactory();
36+
IoHandler handler = ioHandlerFactory.newHandler(new ThreadAwareExecutor() {
37+
38+
@Override
39+
public boolean isExecutorThread(Thread thread) {
40+
return true;
41+
}
42+
43+
@Override
44+
public void execute(Runnable command) {
45+
command.run();
46+
}
47+
});
48+
handler.initialize();
49+
50+
LinuxSocket socket = LinuxSocket.newSocketStream();
51+
AtomicReference<IoEvent> eventRef = new AtomicReference<>();
52+
IoRegistration registration = handler.register(new EpollIoHandle() {
53+
@Override
54+
public FileDescriptor fd() {
55+
return socket;
56+
}
57+
58+
@Override
59+
public void handle(IoRegistration registration, IoEvent ioEvent) {
60+
eventRef.set(ioEvent);
61+
}
62+
63+
@Override
64+
public void close() {
65+
}
66+
});
67+
handler.run(new IoHandlerContext() {
68+
@Override
69+
public boolean canBlock() {
70+
return false;
71+
}
72+
73+
@Override
74+
public long delayNanos(long currentTimeNanos) {
75+
return 0;
76+
}
77+
78+
@Override
79+
public long deadlineNanos() {
80+
return 0;
81+
}
82+
});
83+
assertTrue(registration.cancel());
84+
handler.prepareToDestroy();
85+
handler.destroy();
86+
socket.close();
87+
assertNull(eventRef.get());
88+
}
89+
}

0 commit comments

Comments
0 (0)
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.