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 race between suspend and cancellation … · netty/netty@c83c9c5

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
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,13 @@ public void run() {
12191219
continue;
12201220
}
12211221
suspend = true;
1222+
} else if (currentState == ST_SUSPENDING || currentState == ST_SUSPENDED) {
1223+
// We were trying to suspend (or just did) but something raced in - e.g. a scheduled
1224+
// task being cancelled and re-submitted itself for removal via scheduleRemoveScheduled()
1225+
// - so we can no longer suspend right now. No shutdown was requested, so this must not
1226+
// be treated as if run() returned without confirming shutdown; just go around the loop
1227+
// again so run() can pick up whatever raced in.
1228+
continue;
12221229
}
12231230
break;
12241231
}
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
@@ -207,6 +207,54 @@ void testNotSuspendedUntilScheduledTaskIsCancelled() throws Exception {
207207
}
208208
}
209209

210+
@Test
211+
@Timeout(value = 30, unit = TimeUnit.SECONDS)
212+
void testSuspendDoesNotRaceWithCancelledScheduledTask() throws Exception {
213+
// Regression test: cancelling a scheduled task right as the executor is about to suspend used to
214+
// be able to race with the suspend confirmation in doStartThread(). The cancelled task schedules
215+
// its own removal via scheduleRemoveScheduled(), which can land in the task queue in the tiny
216+
// window between run() deciding it can suspend and doStartThread() re-checking canSuspend().
217+
// That caused the executor to treat the situation as if run() returned without confirming
218+
// shutdown, spuriously shutting itself down instead of suspending (or continuing to run).
219+
// The race is timing dependent, so we repeat the scenario many times to make a regression likely
220+
// to be caught.
221+
for (int i = 0; i < 2000; i++) {
222+
TestThreadFactory threadFactory = new TestThreadFactory();
223+
final SingleThreadEventExecutor executor = new SuspendingSingleThreadEventExecutor(threadFactory);
224+
225+
Future<?> future = executor.schedule(() -> { }, 1, TimeUnit.DAYS);
226+
TestThread currentThread = threadFactory.threads.take();
227+
currentThread.awaitStarted();
228+
currentThread.awaitRunnableExecution();
229+
assertTrue(executor.trySuspend(), "iteration " + i);
230+
231+
assertTrue(future.cancel(false), "iteration " + i);
232+
future.await();
233+
234+
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(2);
235+
while (!executor.trySuspend()) {
236+
if (System.nanoTime() > deadline) {
237+
fail("executor got stuck instead of suspending at iteration " + i
238+
+ " (isShutdown=" + executor.isShutdown() + ')');
239+
}
240+
Thread.sleep(1);
241+
}
242+
243+
currentThread.join(TimeUnit.SECONDS.toMillis(2));
244+
assertFalse(currentThread.isAlive(), "worker thread did not terminate at iteration " + i);
245+
246+
assertTrue(executor.trySuspend(), "iteration " + i + " (isShutdown=" + executor.isShutdown() + ')');
247+
assertTrue(executor.isSuspended(), "iteration " + i);
248+
249+
executor.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).syncUninterruptibly();
250+
251+
TestThread t;
252+
while ((t = threadFactory.threads.poll()) != null) {
253+
t.join();
254+
}
255+
}
256+
}
257+
210258
@Test
211259
void testNotSuspendedUntilScheduledTaskDidRun() throws Exception {
212260
TestThreadFactory threadFactory = new TestThreadFactory();

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.