fix: clear timeout timer in runSource to prevent event loop hang

The Promise.race timeout was never cleared on success/failure,
keeping the Node event loop alive for ~30s after fast sweeps.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Firdavs
2026-03-18 07:40:27 +00:00
parent d22f36e158
commit 6b614d559d

View File

@@ -47,15 +47,18 @@ const SOURCE_TIMEOUT_MS = 30_000; // 30s max per individual source
export async function runSource(name, fn, ...args) {
const start = Date.now();
let timer;
try {
const dataPromise = fn(...args);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error(`Source ${name} timed out after ${SOURCE_TIMEOUT_MS / 1000}s`)), SOURCE_TIMEOUT_MS)
);
const timeoutPromise = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`Source ${name} timed out after ${SOURCE_TIMEOUT_MS / 1000}s`)), SOURCE_TIMEOUT_MS);
});
const data = await Promise.race([dataPromise, timeoutPromise]);
return { name, status: 'ok', durationMs: Date.now() - start, data };
} catch (e) {
return { name, status: 'error', durationMs: Date.now() - start, error: e.message };
} finally {
clearTimeout(timer);
}
}