diff --git a/src/update-check.ts b/src/update-check.ts index ee3d59e..e6bf9bd 100644 --- a/src/update-check.ts +++ b/src/update-check.ts @@ -198,24 +198,34 @@ export async function finishBackgroundUpdateCheck( return; } - const latest = await Promise.race([ - checkPromise, - new Promise((resolve) => - setTimeout(() => resolve(null), updateCheckHTTPTimeoutMs), - ), - ]); - checkPromise = null; - - if (!latest || !isNewerVersion(latest, Version)) { - return; - } + let timeout: ReturnType | undefined; + try { + const latest = await Promise.race([ + checkPromise, + new Promise((resolve) => { + timeout = setTimeout(() => resolve(null), updateCheckHTTPTimeoutMs); + timeout.unref?.(); + }), + ]); + + if (!latest || !isNewerVersion(latest, Version)) { + return; + } - write( - `\nA new version of vanta is available: ${displayVersion(latest)} (you have ${displayVersion(Version)})\nUpdate with:\n curl -fsSL ${installScriptURL} | bash\n\n`, - ); + write( + `\nA new version of vanta is available: ${displayVersion(latest)} (you have ${displayVersion(Version)})\nUpdate with:\n curl -fsSL ${installScriptURL} | bash\n\n`, + ); + } finally { + if (timeout !== undefined) { + clearTimeout(timeout); + } + checkPromise = null; + } } -/** Test helper to clear in-flight check state. */ -export function resetUpdateCheckStateForTests(): void { - checkPromise = null; +/** Test helper to seed or clear in-flight check state. */ +export function resetUpdateCheckStateForTests( + promise: Promise | null = null, +): void { + checkPromise = promise; }