Skip to content

Commit a60c207

Browse files
committed
Apply timeouts to forceful shutdown
1 parent d79d204 commit a60c207

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/main.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,18 @@ export function _runTaskList(
932932
});
933933
try {
934934
logger.debug(`Attempting forceful shutdown`);
935+
const timeout = new Promise((_resolve, reject) => {
936+
const t = setTimeout(
937+
() => reject(new Error("Timed out")),
938+
5000 /* TODO: make configurable */,
939+
);
940+
t.unref();
941+
});
942+
935943
// Stop new jobs being added
936-
const deactivatePromise = deactivate();
944+
// NOTE: deactivate() immediately stops getJob working, even if the
945+
// promise takes a while to resolve.
946+
const deactivatePromise = Promise.race([deactivate(), timeout]);
937947

938948
const errors: Error[] = [];
939949

@@ -945,10 +955,12 @@ export function _runTaskList(
945955

946956
// Remove all the workers - we're shutting them down manually
947957
const workerPromises = workers.map((worker) =>
948-
worker.release(true),
958+
// Note force=true means that this completes immediately _except_
959+
// it still calls the `stopWorker` async hook, so we must still
960+
// handle a timeout.
961+
Promise.race([worker.release(true), timeout]),
949962
);
950963
// Ignore the results, we're shutting down anyway
951-
// TODO: add a timeout
952964
const [deactivateResult, ..._ignoreWorkerReleaseResults] =
953965
await Promise.allSettled([deactivatePromise, ...workerPromises]);
954966
if (deactivateResult.status === "rejected") {
@@ -1026,7 +1038,6 @@ export function _runTaskList(
10261038
},
10271039
);
10281040

1029-
// This should never call fin() since forceful shutdown always errors
10301041
forcefulShutdownPromise.then(fin, finWithError);
10311042

10321043
return forcefulShutdownPromise;

0 commit comments

Comments
 (0)