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 — Don't strand tasks when trySuspend() races with startThread() (#17608) · netty/netty@4e8d377

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
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ protected final void reject(Runnable task) {
11071107

11081108
private void startThread() {
11091109
int currentState = state;
1110-
if (currentState == ST_NOT_STARTED || currentState == ST_SUSPENDED) {
1110+
while (currentState == ST_NOT_STARTED || currentState == ST_SUSPENDED) {
11111111
if (STATE_UPDATER.compareAndSet(this, currentState, ST_STARTED)) {
11121112
resetIdleCycles();
11131113
resetBusyCycles();
@@ -1120,7 +1120,14 @@ private void startThread() {
11201120
STATE_UPDATER.compareAndSet(this, ST_STARTED, ST_NOT_STARTED);
11211121
}
11221122
}
1123+
break;
11231124
}
1125+
// The state changed after we read it. If trySuspend() moved it from ST_NOT_STARTED to ST_SUSPENDED no
1126+
// thread was started, so we need to try again. Otherwise the task that was just added might never run.
1127+
// This may deliberately un-suspend an executor that trySuspend() has just suspended, which is the same
1128+
// thing execute() does for an executor that is already suspended. We only loop again if another thread
1129+
// changed the state in between, so this can't spin.
1130+
currentState = state;
11241131
}
11251132
}
11261133

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
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,54 @@ void testSuspensionWhenExecutorIsNotStarted() throws Exception {
167167
}
168168
}
169169

170+
@Test
171+
@Timeout(value = 30, unit = TimeUnit.SECONDS)
172+
void testExecuteRacingTrySuspendWhenExecutorIsNotStarted() throws Exception {
173+
// execute() on a never started executor races with trySuspend(), which moves the executor from
174+
// ST_NOT_STARTED to ST_SUSPENDED. Whatever the interleaving, execute() must request a thread, otherwise the
175+
// task it just added is stranded. The race is timing dependent, so repeat it many times.
176+
final AtomicReference<SingleThreadEventExecutor> executorRef = new AtomicReference<>();
177+
final AtomicInteger suspended = new AtomicInteger();
178+
final AtomicBoolean done = new AtomicBoolean();
179+
Thread suspender = new Thread(() -> {
180+
while (!done.get()) {
181+
SingleThreadEventExecutor executor = executorRef.getAndSet(null);
182+
if (executor == null) {
183+
Thread.yield();
184+
} else {
185+
executor.trySuspend();
186+
suspended.incrementAndGet();
187+
}
188+
}
189+
});
190+
suspender.start();
191+
try {
192+
for (int i = 1; i <= 10000; i++) {
193+
final AtomicInteger threadStarts = new AtomicInteger();
194+
// Only count the requests to start a thread, no thread is ever started.
195+
final SingleThreadEventExecutor executor = new SingleThreadEventExecutor(
196+
null, (Executor) command -> threadStarts.incrementAndGet(), false, true,
197+
Integer.MAX_VALUE, RejectedExecutionHandlers.reject()) {
198+
@Override
199+
protected void run() {
200+
throw new AssertionError();
201+
}
202+
};
203+
executorRef.set(executor);
204+
executor.execute(() -> { });
205+
while (suspended.get() != i) {
206+
Thread.yield();
207+
}
208+
final int iteration = i;
209+
assertEquals(1, threadStarts.get(), () -> "iteration " + iteration + " (isSuspended="
210+
+ executor.isSuspended() + ')');
211+
}
212+
} finally {
213+
done.set(true);
214+
suspender.join();
215+
}
216+
}
217+
170218
@Test
171219
@Timeout(value = 10, unit = TimeUnit.SECONDS)
172220
void testNotSuspendedUntilScheduledTaskIsCancelled() throws Exception {

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.