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@bcc636d

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
@@ -1174,6 +1174,13 @@ public void run() {
11741174
continue;
11751175
}
11761176
suspend = true;
1177+
} else if (currentState == ST_SUSPENDING || currentState == ST_SUSPENDED) {
1178+
// We were trying to suspend (or just did) but something raced in - e.g. a scheduled
1179+
// task being cancelled and re-submitted itself for removal via scheduleRemoveScheduled()
1180+
// - so we can no longer suspend right now. No shutdown was requested, so this must not
1181+
// be treated as if run() returned without confirming shutdown; just go around the loop
1182+
// again so run() can pick up whatever raced in.
1183+
continue;
11771184
}
11781185
break;
11791186
}
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
@@ -206,6 +206,54 @@ void testNotSuspendedUntilScheduledTaskIsCancelled() throws Exception {
206206
}
207207
}
208208

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