Use a more Promise-oriented approach for the timeout system

This commit is contained in:
Henry Mercer 2023-01-19 20:25:55 +00:00
parent ffb06d7053
commit 85f0d840c9
3 changed files with 31 additions and 38 deletions

33
lib/util.js generated
View file

@ -453,8 +453,8 @@ async function bundleDb(config, language, codeql, dbName) {
}
exports.bundleDb = bundleDb;
async function delay(milliseconds) {
// Immediately `unref` the timer such that it doesn't hold up the event loop
// if it's not being awaited.
// Immediately `unref` the timer such that it only prevents the process from exiting if the
// surrounding promise is being awaited.
return new Promise((resolve) => setTimeout(resolve, milliseconds).unref());
}
exports.delay = delay;
@ -648,22 +648,19 @@ async function withTimeout(timeoutMs, promise, onTimeout) {
finished = true;
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
// to continue, the process won't normally exit until the asynchronous
// task in the background has finished. We set this variable to force
// an exit at the end of our code when `checkForTimeout` is called.
hadTimeout = true;
onTimeout();
}
resolve(undefined);
}, timeoutMs).unref();
});
return await Promise.race([mainTask(), timeout]);
const timeoutTask = async () => {
await delay(timeoutMs);
if (!finished) {
// Workaround: While the promise racing below will allow the main code
// to continue, the process won't normally exit until the asynchronous
// task in the background has finished. We set this variable to force
// an exit at the end of our code when `checkForTimeout` is called.
hadTimeout = true;
onTimeout();
}
return undefined;
};
return await Promise.race([mainTask(), timeoutTask()]);
}
exports.withTimeout = withTimeout;
/**

File diff suppressed because one or more lines are too long

View file

@ -552,8 +552,8 @@ export async function bundleDb(
}
export async function delay(milliseconds: number) {
// Immediately `unref` the timer such that it doesn't hold up the event loop
// if it's not being awaited.
// Immediately `unref` the timer such that it only prevents the process from exiting if the
// surrounding promise is being awaited.
return new Promise((resolve) => setTimeout(resolve, milliseconds).unref());
}
@ -768,23 +768,19 @@ export async function withTimeout<T>(
finished = true;
return result;
};
const timeout: Promise<undefined> = 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
// to continue, the process won't normally exit until the asynchronous
// task in the background has finished. We set this variable to force
// an exit at the end of our code when `checkForTimeout` is called.
hadTimeout = true;
onTimeout();
}
resolve(undefined);
}, timeoutMs).unref();
});
return await Promise.race([mainTask(), timeout]);
const timeoutTask = async () => {
await delay(timeoutMs);
if (!finished) {
// Workaround: While the promise racing below will allow the main code
// to continue, the process won't normally exit until the asynchronous
// task in the background has finished. We set this variable to force
// an exit at the end of our code when `checkForTimeout` is called.
hadTimeout = true;
onTimeout();
}
return undefined;
};
return await Promise.race([mainTask(), timeoutTask()]);
}
/**