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 — SingleThreadEventExecutor: fix execute/suspend race stranding tasks · netty/netty@3373ed5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • common/src
    • main/java/io/netty/util/concurrent
      • SingleThreadEventExecutor.java
    • test/java/io/netty/util/concurrent
      • SingleThreadEventExecutorTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java
+17-11Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ public void run() {
135135

136136
private final Promise<?> terminationFuture = new DefaultPromise<Void>(GlobalEventExecutor.INSTANCE);
137137

138+
private final Runnable neverStartedQuietPeriodTask = new Runnable() {
139+
@Override
140+
public void run() {
141+
onNeverStartedQuietPeriodTimer();
142+
}
143+
};
144+
138145
/**
139146
* Create a new instance
140147
*
@@ -1164,6 +1171,11 @@ private void startThread() {
11641171
try {
11651172
issueDoStartThread();
11661173
success = true;
1174+
// execute() may have moved ST_SUSPENDED -> ST_STARTED while this thread is still
1175+
// winding down; wake it so it re-engages instead of exiting with no replacement.
1176+
if (currentState == ST_SUSPENDED && thread != null) {
1177+
wakeup(false);
1178+
}
11671179
} finally {
11681180
if (!success) {
11691181
STATE_UPDATER.compareAndSet(this, ST_STARTED, ST_NOT_STARTED);
@@ -1258,13 +1270,6 @@ private void scheduleNeverStartedQuietPeriodExpire() {
12581270
}
12591271
}
12601272

1261-
private final Runnable neverStartedQuietPeriodTask = new Runnable() {
1262-
@Override
1263-
public void run() {
1264-
onNeverStartedQuietPeriodTimer();
1265-
}
1266-
};
1267-
12681273
private void onNeverStartedQuietPeriodTimer() {
12691274
if (thread != null || isTerminated() || !isShuttingDown()) {
12701275
return;
@@ -1376,10 +1381,11 @@ public void run() {
13761381
continue;
13771382
}
13781383

1379-
if (!canSuspend(ST_SUSPENDED) && STATE_UPDATER.compareAndSet(SingleThreadEventExecutor.this,
1380-
ST_SUSPENDED, ST_STARTED)) {
1381-
// Seems like there was something added to the task queue again in the meantime but we
1382-
// were able to re-engage this thread as the event loop thread.
1384+
if (!canSuspend(ST_SUSPENDED)) {
1385+
// Work arrived while suspending. execute() may already have moved us to
1386+
// ST_STARTED; re-engage this thread in either case.
1387+
STATE_UPDATER.compareAndSet(SingleThreadEventExecutor.this,
1388+
ST_SUSPENDED, ST_STARTED);
13831389
continue;
13841390
}
13851391
suspend = true;
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
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,79 @@ public Thread newThread(@NotNull Runnable r) {
799799
}
800800
}
801801

802+
/**
803+
* execute() racing suspension must not leave the executor in ST_STARTED with no thread.
804+
*/
805+
@Test
806+
@Timeout(value = 10, unit = TimeUnit.SECONDS)
807+
public void testExecuteRacingSuspensionDoesNotStrandExecutor() throws Exception {
808+
final AtomicBoolean armed = new AtomicBoolean();
809+
final AtomicInteger armedCanSuspendCalls = new AtomicInteger();
810+
final CountDownLatch executorThreadAtReengage = new CountDownLatch(1);
811+
final CountDownLatch externalDone = new CountDownLatch(1);
812+
813+
final SingleThreadEventExecutor executor = new SingleThreadEventExecutor(
814+
null, new DefaultThreadFactory("suspend-race"), false, true,
815+
Integer.MAX_VALUE, RejectedExecutionHandlers.reject()) {
816+
@Override
817+
protected void run() {
818+
for (;;) {
819+
if (confirmShutdown()) {
820+
return;
821+
}
822+
Runnable task = takeTask();
823+
if (task != null) {
824+
task.run();
825+
}
826+
if (isSuspended()) {
827+
return;
828+
}
829+
}
830+
}
831+
832+
@Override
833+
protected boolean canSuspend(int state) {
834+
if (armed.get() && armedCanSuspendCalls.incrementAndGet() == 2) {
835+
executorThreadAtReengage.countDown();
836+
boolean interrupted = false;
837+
while (externalDone.getCount() > 0) {
838+
try {
839+
externalDone.await();
840+
} catch (InterruptedException e) {
841+
interrupted = true;
842+
}
843+
}
844+
if (interrupted) {
845+
Thread.currentThread().interrupt();
846+
}
847+
}
848+
return super.canSuspend(state);
849+
}
850+
851+
@Override
852+
protected void wakeup(boolean inEventLoop) {
853+
interruptThread();
854+
}
855+
};
856+
857+
LatchTask started = new LatchTask();
858+
executor.execute(started);
859+
started.await();
860+
861+
armed.set(true);
862+
assertTrue(executor.trySuspend());
863+
assertTrue(executorThreadAtReengage.await(5, TimeUnit.SECONDS));
864+
865+
LatchTask raced = new LatchTask();
866+
executor.execute(raced);
867+
868+
externalDone.countDown();
869+
870+
assertTrue(raced.await(10, TimeUnit.SECONDS));
871+
872+
executor.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).syncUninterruptibly();
873+
}
874+
802875
/**
803876
* A never-started executor that receives a task before shutdown still starts a thread and runs it.
804877
*/

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.