@@ -1232,7 +1232,10 @@ private boolean ensureThreadStarted(int oldState) { |
1232 | 1232 |
|
1233 | 1233 | if (oldState == ST_NOT_STARTED || oldState == ST_SUSPENDED) { |
1234 | 1234 | try { |
1235 | | - issueDoStartThread(); |
| 1235 | + // Not gated by threadStartIssued. A worker that is still winding down may hold |
| 1236 | + // that flag; skipping here would leave shutdown waiting for a thread that never |
| 1237 | + // starts. processingLock serializes the two runnables. |
| 1238 | + doStartThread(); |
1236 | 1239 | } catch (Throwable cause) { |
1237 | 1240 | STATE_UPDATER.set(this, ST_TERMINATED); |
1238 | 1241 | terminationFuture.tryFailure(cause); |
@@ -1304,39 +1307,62 @@ private void onNeverStartedQuietPeriodTimer() { |
1304 | 1307 | * If this executor was never started and has no pending work, transition straight to |
1305 | 1308 | * {@link #ST_TERMINATED} without creating a thread. |
1306 | 1309 | * |
1307 | | - * @return {@code true} if termination completed (or was already complete) without starting |
1308 | | - * a thread; {@code false} if a thread still needs to be started to drain work / run shutdown. |
| 1310 | + * @return {@code true} if the caller must not start a thread: termination completed |
| 1311 | + * without a worker, or this method already issued one for work that was queued before |
| 1312 | + * {@link #ST_SHUTDOWN}. {@code false} if the caller should start a thread. |
1309 | 1313 | */ |
1310 | 1314 | private boolean tryTerminateIfNeverStarted() { |
1311 | 1315 | // Try to take the processing lock so we do not race with a just-started worker thread. |
1312 | 1316 | // If the lock is held, a thread is (or will be) running — fall back to the normal path. |
1313 | 1317 | if (!processingLock.tryLock()) { |
1314 | 1318 | return false; |
1315 | 1319 | } |
| 1320 | + boolean claimedStart = false; |
| 1321 | + boolean keepClaim = false; |
1316 | 1322 | try { |
1317 | | - if (thread != null) { |
| 1323 | + if (thread != null || hasPendingWork()) { |
1318 | 1324 | return false; |
1319 | 1325 | } |
1320 | | - // Re-check work under the lock. taskQueue / scheduled queue may have received a task |
1321 | | - // after shutdown0 observed ST_NOT_STARTED (execute accepts tasks while SHUTTING_DOWN). |
1322 | | - if (!taskQueue.isEmpty() || nextScheduledTaskDeadlineNanos() != -1 || !shutdownHooks.isEmpty()) { |
| 1326 | + // issueDoStartThread() and this termination must exclude each other. A worker that |
| 1327 | + // already passed the gate is waiting on processingLock; do not terminate under it. |
| 1328 | + if (!threadStartIssued.compareAndSet(false, true)) { |
1323 | 1329 | return false; |
1324 | 1330 | } |
| 1331 | + claimedStart = true; |
1325 | 1332 |
|
1326 | 1333 | for (;;) { |
1327 | 1334 | int currentState = state; |
1328 | 1335 | if (currentState >= ST_TERMINATED) { |
| 1336 | + keepClaim = true; |
1329 | 1337 | return true; |
1330 | 1338 | } |
1331 | 1339 | if (currentState < ST_SHUTTING_DOWN) { |
1332 | 1340 | // Unexpected: state moved backwards from shutting down. Fall back. |
1333 | 1341 | return false; |
1334 | 1342 | } |
1335 | | - // Work may have appeared after the empty check above. |
1336 | | - if (!taskQueue.isEmpty() || nextScheduledTaskDeadlineNanos() != -1 || !shutdownHooks.isEmpty()) { |
| 1343 | + if (hasPendingWork()) { |
1337 | 1344 | return false; |
1338 | 1345 | } |
| 1346 | + // SHUTTING_DOWN still accepts tasks. Publish SHUTDOWN before the last emptiness |
| 1347 | + // check so an execute() in that window is rejected instead of queued and then |
| 1348 | + // discarded by drainTasks(), which also used to let a worker run after cleanup(). |
| 1349 | + if (currentState == ST_SHUTTING_DOWN) { |
| 1350 | + if (!STATE_UPDATER.compareAndSet(this, ST_SHUTTING_DOWN, ST_SHUTDOWN)) { |
| 1351 | + continue; |
| 1352 | + } |
| 1353 | + currentState = ST_SHUTDOWN; |
| 1354 | + } |
| 1355 | + if (hasPendingWork()) { |
| 1356 | + // Accepted before ST_SHUTDOWN. startThread() will not start a worker once the |
| 1357 | + // state has left SHUTTING_DOWN, so issue one here. |
| 1358 | + threadStartIssued.set(false); |
| 1359 | + claimedStart = false; |
| 1360 | + issueDoStartThread(); |
| 1361 | + keepClaim = true; |
| 1362 | + return true; |
| 1363 | + } |
1339 | 1364 | if (STATE_UPDATER.compareAndSet(this, currentState, ST_TERMINATED)) { |
| 1365 | + keepClaim = true; |
1340 | 1366 | try { |
1341 | 1367 | // Subclass cleanup (e.g. SingleThreadIoEventLoop) may assert |
1342 | 1368 | // inEventLoop() and must still destroy constructor-created |
@@ -1363,10 +1389,17 @@ private boolean tryTerminateIfNeverStarted() { |
1363 | 1389 | } |
1364 | 1390 | } |
1365 | 1391 | } finally { |
| 1392 | + if (claimedStart && !keepClaim) { |
| 1393 | + threadStartIssued.set(false); |
| 1394 | + } |
1366 | 1395 | processingLock.unlock(); |
1367 | 1396 | } |
1368 | 1397 | } |
1369 | 1398 |
|
| 1399 | + private boolean hasPendingWork() { |
| 1400 | + return !taskQueue.isEmpty() || nextScheduledTaskDeadlineNanos() != -1 || !shutdownHooks.isEmpty(); |
| 1401 | + } |
| 1402 | + |
1370 | 1403 | private void doStartThread() { |
1371 | 1404 | executor.execute(new Runnable() { |
1372 | 1405 | @Override |
@@ -1442,10 +1475,12 @@ public void run() { |
1442 | 1475 | } |
1443 | 1476 |
|
1444 | 1477 | try { |
1445 | | - if (shutdown) { |
| 1478 | + if (shutdown && state != ST_TERMINATED) { |
1446 | 1479 | // Run all remaining tasks and shutdown hooks. At this point the event loop |
1447 | 1480 | // is in ST_SHUTTING_DOWN state still accepting tasks which is needed for |
1448 | 1481 | // graceful shutdown with quietPeriod. |
| 1482 | + // A second worker can be inside doStartThread() after the first already |
| 1483 | + // published ST_TERMINATED; it must not complete the future again. |
1449 | 1484 | for (;;) { |
1450 | 1485 | if (confirmShutdown()) { |
1451 | 1486 | break; |
@@ -1468,7 +1503,7 @@ public void run() { |
1468 | 1503 | } |
1469 | 1504 | } finally { |
1470 | 1505 | try { |
1471 | | - if (shutdown) { |
| 1506 | + if (shutdown && state != ST_TERMINATED) { |
1472 | 1507 | try { |
1473 | 1508 | cleanup(); |
1474 | 1509 | } finally { |
@@ -1510,7 +1545,9 @@ public void run() { |
1510 | 1545 | int currentState = state; |
1511 | 1546 | if (currentState == ST_STARTED |
1512 | 1547 | || (currentState >= ST_SHUTTING_DOWN && currentState < ST_TERMINATED)) { |
1513 | | - issueDoStartThread(); |
| 1548 | + // Direct start: issueDoStartThread() would no-op if this winding-down |
| 1549 | + // worker had not cleared the gate yet, or if a racer still holds it. |
| 1550 | + doStartThread(); |
1514 | 1551 | } |
1515 | 1552 | } |
1516 | 1553 | } |
|