diff --git a/lib/processors/minifier.js b/lib/processors/minifier.js index 76b2564b2..fc0b022d1 100644 --- a/lib/processors/minifier.js +++ b/lib/processors/minifier.js @@ -28,14 +28,20 @@ function getPool(taskUtil) { workerType: "auto", maxWorkers }); - taskUtil.registerCleanupTask(() => { + taskUtil.registerCleanupTask((options) => { const attemptPoolTermination = () => { log.verbose(`Attempt to terminate the workerpool...`); + if (!pool) { + return; + } + + const forceTerminate = !!options && !!options.exitCode; + // There are many stats that could be used, but these ones seem the most // convenient. When all the (available) workers are idle, then it's safe to terminate. const {idleWorkers, totalWorkers} = pool.stats(); - if (idleWorkers !== totalWorkers) { + if (idleWorkers !== totalWorkers && !forceTerminate) { return new Promise((resolve) => setTimeout(() => resolve(attemptPoolTermination()), 100) // Retry after a while ); @@ -43,7 +49,7 @@ function getPool(taskUtil) { const poolToBeTerminated = pool; pool = null; - return poolToBeTerminated.terminate(); + return poolToBeTerminated.terminate(forceTerminate); }; return attemptPoolTermination(); diff --git a/lib/tasks/buildThemes.js b/lib/tasks/buildThemes.js index f3c1b263a..ce410d40b 100644 --- a/lib/tasks/buildThemes.js +++ b/lib/tasks/buildThemes.js @@ -23,14 +23,20 @@ function getPool(taskUtil) { workerType: "thread", maxWorkers }); - taskUtil.registerCleanupTask(() => { + taskUtil.registerCleanupTask((options) => { const attemptPoolTermination = () => { log.verbose(`Attempt to terminate the workerpool...`); + if (!pool) { + return; + } + + const forceTerminate = !!options && !!options.exitCode; + // There are many stats that could be used, but these ones seem the most // convenient. When all the (available) workers are idle, then it's safe to terminate. const {idleWorkers, totalWorkers} = pool.stats(); - if (idleWorkers !== totalWorkers) { + if (idleWorkers !== totalWorkers && !forceTerminate) { return new Promise((resolve) => setTimeout(() => resolve(attemptPoolTermination()), 100) // Retry after a while ); @@ -38,7 +44,7 @@ function getPool(taskUtil) { const poolToBeTerminated = pool; pool = null; - return poolToBeTerminated.terminate(); + return poolToBeTerminated.terminate(forceTerminate); }; return attemptPoolTermination();