@@ -1166,12 +1166,24 @@ private void startThread() { |
1166 | 1166 | } |
1167 | 1167 | } |
1168 | 1168 |
|
| 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 | + |
1169 | 1178 | 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()) { |
1175 | 1187 | return true; |
1176 | 1188 | } |
1177 | 1189 |
|
@@ -1195,6 +1207,9 @@ private boolean ensureThreadStarted(int oldState) { |
1195 | 1207 | /** |
1196 | 1208 | * If this executor was never started and has no pending work, transition straight to |
1197 | 1209 | * {@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. |
1198 | 1213 | * |
1199 | 1214 | * @return {@code true} if termination completed (or was already complete) without starting |
1200 | 1215 | * a thread; {@code false} if a thread still needs to be started to drain work / run shutdown. |
@@ -1230,7 +1245,18 @@ private boolean tryTerminateIfNeverStarted() { |
1230 | 1245 | } |
1231 | 1246 | if (STATE_UPDATER.compareAndSet(this, currentState, ST_TERMINATED)) { |
1232 | 1247 | 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 | + } |
1234 | 1260 | } finally { |
1235 | 1261 | threadLock.countDown(); |
1236 | 1262 | int numUserTasks = drainTasks(); |
|