fix(core): clear 5-minute timeouts in oauth flow to prevent memory leaks (#24968)

This commit is contained in:
Spencer
2026-04-09 17:14:07 -04:00
committed by GitHub
parent f744913584
commit 0f7f7be4ef
4 changed files with 81 additions and 30 deletions

View File

@@ -424,6 +424,7 @@ async function authWithUserCode(client: OAuth2Client): Promise<boolean> {
'\n\n',
);
let authTimeoutId: NodeJS.Timeout | undefined;
const code = await new Promise<string>((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
@@ -431,20 +432,29 @@ async function authWithUserCode(client: OAuth2Client): Promise<boolean> {
terminal: true,
});
const timeout = setTimeout(() => {
rl.close();
reject(
const abortController = new AbortController();
authTimeoutId = setTimeout(() => {
abortController.abort(
new FatalAuthenticationError(
'Authorization timed out after 5 minutes.',
),
);
}, 300000); // 5 minute timeout
authTimeoutId.unref();
const onAbort = () => {
rl.close();
reject(abortController.signal.reason);
};
abortController.signal.addEventListener('abort', onAbort, { once: true });
rl.question('Enter the authorization code: ', (code) => {
clearTimeout(timeout);
abortController.signal.removeEventListener('abort', onAbort);
rl.close();
resolve(code.trim());
});
}).finally(() => {
if (authTimeoutId) clearTimeout(authTimeoutId);
});
if (!code) {