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 never-started shutdown for IoEventLoop · netty/netty@d515a51

File tree

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

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

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/MultithreadEventExecutorGroup.java
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,16 @@ public List<AutoScalingUtilizationMetric> executorUtilizations() {
188188
@Override
189189
public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
190190
for (EventExecutor l: children) {
191-
l.shutdownGracefully(quietPeriod, timeout, unit);
191+
// Never-started children have no work to drain. Force a zero quiet period so
192+
// SingleThreadEventExecutor can terminate without creating a worker thread that
193+
// would only exit immediately (#17135). Children that already run still honor
194+
// the caller's quietPeriod/timeout.
195+
if (l instanceof SingleThreadEventExecutor
196+
&& ((SingleThreadEventExecutor) l).isNeverStarted()) {
197+
l.shutdownGracefully(0, 0, unit);
198+
} else {
199+
l.shutdownGracefully(quietPeriod, timeout, unit);
200+
}
192201
}
193202
return terminationFuture();
194203
}
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
+32-6Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,12 +1166,24 @@ private void startThread() {
11661166
}
11671167
}
11681168

1169+
/**
1170+
* Returns {@code true} if no worker thread has ever been started for this executor.
1171+
* Used by {@link MultithreadEventExecutorGroup} to skip the quiet period for unused
1172+
* children so they can terminate without creating threads (#17135).
1173+
*/
1174+
final boolean isNeverStarted() {
1175+
return thread == null && state == ST_NOT_STARTED;
1176+
}
1177+
11691178
private boolean ensureThreadStarted(int oldState) {
1170-
// Fast-path for a never-started executor that has nothing to drain: mark terminated
1171-
// without spinning up a worker thread that would only shut down immediately.
1172-
// MultithreadEventExecutorGroup.shutdownGracefully() walks every child; without this,
1173-
// creating a group of N and shutting it down unused starts all N threads (#17135).
1174-
if (oldState == ST_NOT_STARTED && tryTerminateIfNeverStarted()) {
1179+
// Fast-path for a never-started executor with zero quiet period and nothing to drain:
1180+
// mark terminated without spinning up a worker that would only shut down immediately.
1181+
// Non-zero quiet periods still start a thread so tasks submitted during the quiet
1182+
// period are accepted (see SingleThreadEventLoopTest#testGracefulShutdownQuietPeriod).
1183+
// MultithreadEventExecutorGroup forces quietPeriod=0 for never-started children (#17135).
1184+
if (oldState == ST_NOT_STARTED
1185+
&& gracefulShutdownQuietPeriod == 0
1186+
&& tryTerminateIfNeverStarted()) {
11751187
return true;
11761188
}
11771189

@@ -1195,6 +1207,9 @@ private boolean ensureThreadStarted(int oldState) {
11951207
/**
11961208
* If this executor was never started and has no pending work, transition straight to
11971209
* {@link #ST_TERMINATED} without creating a thread.
1210+
* <p>
1211+
* Only used when {@link #gracefulShutdownQuietPeriod} is {@code 0}; otherwise a thread
1212+
* must run to honor the quiet period and accept further tasks until it elapses.
11981213
*
11991214
* @return {@code true} if termination completed (or was already complete) without starting
12001215
* a thread; {@code false} if a thread still needs to be started to drain work / run shutdown.
@@ -1230,7 +1245,18 @@ private boolean tryTerminateIfNeverStarted() {
12301245
}
12311246
if (STATE_UPDATER.compareAndSet(this, currentState, ST_TERMINATED)) {
12321247
try {
1233-
cleanup();
1248+
// Subclass cleanup (e.g. SingleThreadIoEventLoop) may assert
1249+
// inEventLoop() and must still destroy constructor-created
1250+
// resources (IoHandler / native FDs). Bind the current thread
1251+
// as the event-loop thread only for cleanup; run() never ran
1252+
// so there is no concurrent event-loop activity.
1253+
assert thread == null;
1254+
thread = Thread.currentThread();
1255+
try {
1256+
cleanup();
1257+
} finally {
1258+
thread = null;
1259+
}
12341260
} finally {
12351261
threadLock.countDown();
12361262
int numUserTasks = drainTasks();

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.