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 — Fix NPE and AssertionErrors when many tasks are scheduled and cancell… · netty/netty@4109d7b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • common/src
    • main/java/io/netty/util/concurrent
      • AbstractScheduledEventExecutor.java
      • GlobalEventExecutor.java
      • ScheduledFutureTask.java
    • test/java/io/netty/util/concurrent
      • SingleThreadEventExecutorTest.java
  • testsuite/src/main/java/io/netty/testsuite/transport
    • AbstractSingleThreadEventLoopTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ protected void validateScheduled(long amount, TimeUnit unit) {
334334

335335
final void scheduleFromEventLoop(final ScheduledFutureTask<?> task) {
336336
// nextTaskId a long and so there is no chance it will overflow back to 0
337-
scheduledTaskQueue().add(task.setId(++nextTaskId));
337+
if (task.getId() == 0L) {
338+
task.setId(++nextTaskId);
339+
}
340+
scheduledTaskQueue().add(task);
338341
}
339342

340343
private <V> ScheduledFuture<V> schedule(final ScheduledFutureTask<V> task) {
Collapse file

‎common/src/main/java/io/netty/util/concurrent/GlobalEventExecutor.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/GlobalEventExecutor.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void run() {
8484
private final Future<?> terminationFuture;
8585

8686
private GlobalEventExecutor() {
87-
scheduledTaskQueue().add(quietPeriodTask);
87+
scheduleFromEventLoop(quietPeriodTask);
8888
threadFactory = ThreadExecutorMap.apply(new DefaultThreadFactory(
8989
DefaultThreadFactory.toPoolName(getClass()), false, Thread.NORM_PRIORITY, null), this);
9090

Collapse file

‎common/src/main/java/io/netty/util/concurrent/ScheduledFutureTask.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/ScheduledFutureTask.java
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ ScheduledFutureTask<V> setId(long id) {
8080
return this;
8181
}
8282

83+
long getId() {
84+
return id;
85+
}
86+
8387
@Override
8488
protected EventExecutor executor() {
8589
return super.executor();
@@ -167,7 +171,7 @@ public void run() {
167171
deadlineNanos = scheduledExecutor().getCurrentTimeNanos() - periodNanos;
168172
}
169173
if (!isCancelled()) {
170-
scheduledExecutor().scheduledTaskQueue().add(this);
174+
scheduledExecutor().scheduleFromEventLoop(this);
171175
}
172176
}
173177
}
Collapse file

‎common/src/test/java/io/netty/util/concurrent/SingleThreadEventExecutorTest.java‎

Copy file name to clipboardExpand all lines: common/src/test/java/io/netty/util/concurrent/SingleThreadEventExecutorTest.java
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.jetbrains.annotations.NotNull;
1919
import org.junit.jupiter.api.Test;
20-
2120
import org.junit.jupiter.api.Timeout;
2221
import org.junit.jupiter.api.function.Executable;
2322

Collapse file

‎testsuite/src/main/java/io/netty/testsuite/transport/AbstractSingleThreadEventLoopTest.java‎

Copy file name to clipboardExpand all lines: testsuite/src/main/java/io/netty/testsuite/transport/AbstractSingleThreadEventLoopTest.java
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@
2828
import io.netty.util.concurrent.EventExecutor;
2929
import io.netty.util.concurrent.Future;
3030
import io.netty.util.concurrent.Promise;
31+
import io.netty.util.concurrent.ScheduledFuture;
32+
import io.netty.util.internal.PlatformDependent;
3133
import org.junit.jupiter.api.Test;
3234
import org.junit.jupiter.api.Timeout;
3335
import org.junit.jupiter.api.function.Executable;
3436

37+
import java.util.ArrayList;
3538
import java.util.HashSet;
3639
import java.util.Iterator;
40+
import java.util.List;
3741
import java.util.NoSuchElementException;
3842
import java.util.Set;
3943
import java.util.concurrent.Callable;
4044
import java.util.concurrent.CountDownLatch;
4145
import java.util.concurrent.RejectedExecutionException;
4246
import java.util.concurrent.TimeUnit;
4347

48+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4449
import static org.junit.jupiter.api.Assertions.assertEquals;
4550
import static org.junit.jupiter.api.Assertions.assertFalse;
4651
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -266,6 +271,39 @@ public void execute() {
266271
}
267272
}
268273

274+
@Test
275+
void schedulingAndCancellingTasks() throws Exception {
276+
Runnable runnable = new Runnable() {
277+
@Override
278+
public void run() {
279+
}
280+
};
281+
List<ScheduledFuture<?>> tasks = new ArrayList<ScheduledFuture<?>>();
282+
EventLoopGroup group = newEventLoopGroup();
283+
try {
284+
EventLoop eventLoop = group.next();
285+
for (int i = 0; i < 5000; i++) {
286+
tasks.add(eventLoop.scheduleAtFixedRate(runnable, 1, 1, MILLISECONDS));
287+
if (tasks.size() > 500) {
288+
tasks.get(PlatformDependent.threadLocalRandom().nextInt(tasks.size())).cancel(false);
289+
}
290+
}
291+
for (ScheduledFuture<?> task : tasks) {
292+
task.cancel(false);
293+
}
294+
for (ScheduledFuture<?> task : tasks) {
295+
task.await();
296+
}
297+
for (ScheduledFuture<?> task : tasks) {
298+
if (!task.isCancelled()) {
299+
task.sync();
300+
}
301+
}
302+
} finally {
303+
group.shutdownGracefully();
304+
}
305+
}
306+
269307
private static void runBlockingOn(EventLoop eventLoop, final Runnable action) {
270308
final Promise<Void> promise = eventLoop.newPromise();
271309
eventLoop.execute(new Runnable() {

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.