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 — Prevent ManualEventLoop to block on a racy task submission · netty/netty@dcab167

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-classes-epoll/src/main/java/io/netty/channel/epoll
    • EpollIoHandler.java
  • transport-native-epoll/src/test/java/io/netty/channel/epoll
    • ManualEventLoopTest.java
  • transport/src/main/java/io/netty/channel
    • ManualIoEventLoop.java
Expand file treeCollapse file tree
Open diff view settings
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
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class EpollIoHandler implements IoHandler {
6161
}
6262

6363
// Pick a number that no task could have previously used.
64-
private long prevDeadlineNanos = nanoTime() - 1;
64+
private long prevDeadlineNanos = NONE;
6565
private FileDescriptor epollFd;
6666
private FileDescriptor eventFd;
6767
private FileDescriptor timerFd;
Collapse file

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

Copy file name to clipboard
+224Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* Copyright 2025 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.IoEventLoop;
19+
import io.netty.channel.IoEventLoopGroup;
20+
import io.netty.channel.IoHandlerFactory;
21+
import io.netty.channel.ManualIoEventLoop;
22+
import io.netty.channel.MultiThreadIoEventLoopGroup;
23+
import io.netty.util.concurrent.Future;
24+
import org.junit.jupiter.api.RepeatedTest;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.Timeout;
27+
28+
import java.util.Queue;
29+
import java.util.concurrent.ConcurrentLinkedQueue;
30+
import java.util.concurrent.CountDownLatch;
31+
import java.util.concurrent.CyclicBarrier;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.Executor;
34+
import java.util.concurrent.RejectedExecutionException;
35+
import java.util.concurrent.TimeUnit;
36+
import java.util.concurrent.atomic.AtomicLong;
37+
import java.util.function.Consumer;
38+
39+
@Timeout(5)
40+
public class ManualEventLoopTest {
41+
42+
@Test
43+
void firstRacySubmissionMissWakeupEpoll() throws Exception {
44+
racySubmissionMissWakeup(EpollIoHandler.newFactory(), 1);
45+
}
46+
47+
@Test
48+
void secondRacySubmissionMissWakeupEpoll() throws Exception {
49+
racySubmissionMissWakeup(EpollIoHandler.newFactory(), 2);
50+
}
51+
52+
@Test
53+
void firstRacyOtherSubmissionMissWakeupEpoll() throws Exception {
54+
racyOtherSubmissionMissWakeup(EpollIoHandler.newFactory(), 1);
55+
}
56+
57+
@Test
58+
void secondRacyOtherSubmissionMissWakeupEpoll() throws Exception {
59+
racyOtherSubmissionMissWakeup(EpollIoHandler.newFactory(), 2);
60+
}
61+
62+
void racySubmissionMissWakeup(IoHandlerFactory handlerFactory, long canBlockAttempt)
63+
throws Exception {
64+
CyclicBarrier waitBeforeSubmittingTask = new CyclicBarrier(2);
65+
CountDownLatch taskSubmitted = new CountDownLatch(1);
66+
AtomicLong canBlock = new AtomicLong(0);
67+
ManualMultithreadedIoEventLoopGroup group = new ManualMultithreadedIoEventLoopGroup(handlerFactory) {
68+
@Override
69+
protected void beforeCanBlock(Executor executor) {
70+
if (canBlock.incrementAndGet() == canBlockAttempt) {
71+
try {
72+
waitBeforeSubmittingTask.await();
73+
} catch (Throwable ignore) {
74+
//
75+
}
76+
try {
77+
taskSubmitted.await();
78+
} catch (InterruptedException e) {
79+
throw new RuntimeException(e);
80+
}
81+
}
82+
}
83+
};
84+
waitBeforeSubmittingTask.await();
85+
// depending on canBlockAttempt this submission could observe an AWAKE event loop
86+
// or with a setup NONE deadline (e.g. Long.MAX_VALUE).
87+
// In the latter case, it can be already asleep or ready to do it.
88+
Future<?> submitted = group.submit(() -> {
89+
});
90+
// unblock canBlock
91+
taskSubmitted.countDown();
92+
submitted.get();
93+
group.shutdownGracefully(0, 0, TimeUnit.SECONDS).get();
94+
}
95+
96+
void racyOtherSubmissionMissWakeup(IoHandlerFactory handlerFactory, long canBlockAttempt)
97+
throws Exception {
98+
CyclicBarrier waitBeforeSubmittingTask = new CyclicBarrier(2);
99+
CountDownLatch taskSubmitted = new CountDownLatch(1);
100+
AtomicLong canBlock = new AtomicLong(0);
101+
ManualMultithreadedIoEventLoopGroup group = new ManualMultithreadedIoEventLoopGroup(handlerFactory) {
102+
@Override
103+
protected void beforeCanBlock(Executor executor) {
104+
// this should be called when canBlock is called after setting the wakeup flag!
105+
if (canBlock.incrementAndGet() == canBlockAttempt) {
106+
try {
107+
waitBeforeSubmittingTask.await();
108+
} catch (Throwable ignore) {
109+
//
110+
}
111+
try {
112+
taskSubmitted.await();
113+
} catch (InterruptedException e) {
114+
throw new RuntimeException(e);
115+
}
116+
}
117+
}
118+
};
119+
waitBeforeSubmittingTask.await();
120+
CountDownLatch completed = new CountDownLatch(1);
121+
// depending on canBlockAttempt this submission could observe an AWAKE event loop
122+
// or with a setup NONE deadline (e.g. Long.MAX_VALUE).
123+
// In the latter case, it can be already asleep or ready to do it.
124+
group.ioEventLoopRunner.execute(completed::countDown);
125+
taskSubmitted.countDown();
126+
completed.await();
127+
group.shutdownGracefully(0, 0, TimeUnit.SECONDS).get();
128+
}
129+
130+
@RepeatedTest(value = 100, failureThreshold = 1)
131+
void testTightShutodownEpoll() throws InterruptedException, ExecutionException {
132+
ManualMultithreadedIoEventLoopGroup group = new ManualMultithreadedIoEventLoopGroup(
133+
EpollIoHandler.newFactory());
134+
group.shutdownGracefully(0, 0, TimeUnit.SECONDS).get();
135+
}
136+
137+
public static class ManualMultithreadedIoEventLoopGroup extends MultiThreadIoEventLoopGroup {
138+
139+
private ManualIoEventLoopRunner ioEventLoopRunner;
140+
private Consumer<ManualIoEventLoopRunner> beforeCanBlock;
141+
142+
public ManualMultithreadedIoEventLoopGroup(IoHandlerFactory ioHandlerFactory) {
143+
super(1, ioHandlerFactory);
144+
}
145+
146+
@Override
147+
protected IoEventLoop newChild(Executor executor, IoHandlerFactory ioHandlerFactory, Object... args) {
148+
this.ioEventLoopRunner = new ManualIoEventLoopRunner(this, ioHandlerFactory,
149+
executor, this::beforeCanBlock);
150+
return ioEventLoopRunner.ioEventLoop;
151+
}
152+
153+
protected void beforeCanBlock(Executor executor) {
154+
}
155+
156+
private static class ManualIoEventLoopRunner implements Executor {
157+
158+
private final ManualIoEventLoop ioEventLoop;
159+
private final Queue<Runnable> otherTasks = new ConcurrentLinkedQueue<>();
160+
161+
ManualIoEventLoopRunner(IoEventLoopGroup parent, IoHandlerFactory factory,
162+
Executor executor, Consumer<Executor> beforeCanBlock) {
163+
this.ioEventLoop = new ManualIoEventLoop(parent, null, factory) {
164+
@Override
165+
protected boolean canBlock() {
166+
if (beforeCanBlock != null) {
167+
beforeCanBlock.accept(ManualIoEventLoopRunner.this);
168+
}
169+
return otherTasks.isEmpty();
170+
}
171+
};
172+
CountDownLatch started = new CountDownLatch(1);
173+
executor.execute(() -> {
174+
ioEventLoop.setOwningThread(Thread.currentThread());
175+
// it would force a first init
176+
ioEventLoop.runNow();
177+
started.countDown();
178+
mainLoop();
179+
});
180+
try {
181+
started.await();
182+
} catch (InterruptedException e) {
183+
Thread.currentThread().interrupt();
184+
throw new RuntimeException(e);
185+
}
186+
}
187+
188+
private void mainLoop() {
189+
while (!ioEventLoop.isShuttingDown()) {
190+
ioEventLoop.run(0);
191+
Runnable otherTask = otherTasks.poll();
192+
if (otherTask != null) {
193+
safeExecute(otherTask);
194+
}
195+
}
196+
while (!ioEventLoop.isTerminated() || !otherTasks.isEmpty()) {
197+
ioEventLoop.runNow();
198+
Runnable otherTask = otherTasks.poll();
199+
if (otherTask != null) {
200+
safeExecute(otherTask);
201+
}
202+
}
203+
}
204+
205+
private void safeExecute(Runnable task) {
206+
try {
207+
task.run();
208+
} catch (Throwable ignore) {
209+
//
210+
}
211+
}
212+
213+
public void execute(Runnable otherTask) {
214+
otherTasks.add(otherTask);
215+
if (ioEventLoop.isShutdown()) {
216+
if (otherTasks.remove(otherTask)) {
217+
throw new RejectedExecutionException("Event loop shut down");
218+
}
219+
}
220+
ioEventLoop.wakeup();
221+
}
222+
}
223+
}
224+
}
Collapse file

‎transport/src/main/java/io/netty/channel/ManualIoEventLoop.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/ManualIoEventLoop.java
+25-8Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* {@link IoEventLoop} to also do other work. Care must be taken that the {@link #runNow() or
4848
* {@link #waitAndRun()}} methods are called in a timely fashion.
4949
*/
50-
public final class ManualIoEventLoop extends AbstractScheduledEventExecutor implements IoEventLoop {
50+
public class ManualIoEventLoop extends AbstractScheduledEventExecutor implements IoEventLoop {
5151
private static final int ST_STARTED = 0;
5252
private static final int ST_SHUTTING_DOWN = 1;
5353
private static final int ST_SHUTDOWN = 2;
@@ -87,6 +87,14 @@ public long deadlineNanos() {
8787
private long lastExecutionTime;
8888
private boolean initialized;
8989

90+
/**
91+
* This allows to specify additional blocking conditions which will be used by the {@link IoHandler} to decide
92+
* whether it is allowed to block or not.
93+
*/
94+
protected boolean canBlock() {
95+
return true;
96+
}
97+
9098
/**
9199
* Create a new {@link IoEventLoop} that is owned by the user and so needs to be driven by the user with the given
92100
* {@link Thread}. This means that the user is responsible to call either {@link #runNow()} or
@@ -461,6 +469,9 @@ private void shutdown0(long quietPeriod, long timeout, int shutdownState) {
461469
}
462470

463471
if (wakeup) {
472+
// same as AbstractScheduledEventExecutor.WAKEUP_TASK
473+
taskQueue.offer(() -> {
474+
});
464475
handler.wakeup();
465476
}
466477
}
@@ -632,13 +643,14 @@ private void throwIfInEventLoop(String method) {
632643
}
633644
}
634645

635-
private final class BlockingIoHandlerContext implements IoHandlerContext {
646+
private class BlockingIoHandlerContext implements IoHandlerContext {
647+
// this is a positive amount of nanos or Long.MAX_VALUE for no limit
636648
long maxBlockingNanos = Long.MAX_VALUE;
637649

638650
@Override
639651
public boolean canBlock() {
640652
assert inEventLoop();
641-
return !hasTasks() && !hasScheduledTasks();
653+
return !hasTasks() && !hasScheduledTasks() && ManualIoEventLoop.this.canBlock();
642654
}
643655

644656
@Override
@@ -651,11 +663,16 @@ public long delayNanos(long currentTimeNanos) {
651663
public long deadlineNanos() {
652664
assert inEventLoop();
653665
long next = nextScheduledTaskDeadlineNanos();
654-
long maxDeadlineNanos = ticker.nanoTime() + maxBlockingNanos;
655-
if (next == -1) {
656-
return maxDeadlineNanos;
666+
if (maxBlockingNanos == Long.MAX_VALUE) {
667+
// next == -1? -1 : next i.e. return next
668+
return next;
669+
}
670+
long now = ticker.nanoTime();
671+
// we cannot just check Math.min as nanoTime can be negative or wrap around!
672+
if (next == -1 || next - now > maxBlockingNanos) {
673+
return now + maxBlockingNanos;
657674
}
658-
return Math.min(next, maxDeadlineNanos);
675+
return next;
659676
}
660-
};
677+
}
661678
}

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.