Immediately unref timers to avoid waiting for them before exit

This commit is contained in:
Henry Mercer 2023-01-19 18:09:06 +00:00
parent 60e5868d6e
commit b61b299591
3 changed files with 13 additions and 5 deletions

8
lib/util.js generated
View file

@ -453,7 +453,9 @@ async function bundleDb(config, language, codeql, dbName) {
}
exports.bundleDb = bundleDb;
async function delay(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
// Immediately `unref` the timer such that it doesn't hold up the event loop
// if it's not being awaited.
return new Promise((resolve) => setTimeout(resolve, milliseconds).unref());
}
exports.delay = delay;
function isGoodVersion(versionSpec) {
@ -647,6 +649,8 @@ async function withTimeout(timeoutMs, promise, onTimeout) {
return result;
};
const timeout = new Promise((resolve) => {
// Immediately `unref` the timer such that it doesn't hold up the event loop
// if it's not being awaited.
setTimeout(() => {
if (!finished) {
// Workaround: While the promise racing below will allow the main code
@ -657,7 +661,7 @@ async function withTimeout(timeoutMs, promise, onTimeout) {
onTimeout();
}
resolve(undefined);
}, timeoutMs);
}, timeoutMs).unref();
});
return await Promise.race([mainTask(), timeout]);
}