From 1ac35b5d56ff292e5da9e466266b34ce875de397 Mon Sep 17 00:00:00 2001 From: Rod Boev Date: Thu, 5 Feb 2026 10:12:45 -0500 Subject: [PATCH] Clear setTimeout when fetch wins the race Prevents the timer callback from firing unnecessarily after a successful fetch response. --- src/shared/worker-utils.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/shared/worker-utils.ts b/src/shared/worker-utils.ts index b3985e50..e83b7b4b 100644 --- a/src/shared/worker-utils.ts +++ b/src/shared/worker-utils.ts @@ -17,11 +17,15 @@ const HEALTH_CHECK_TIMEOUT_MS = getTimeout(HOOK_TIMEOUTS.HEALTH_CHECK); * The orphaned fetch is harmless since the process exits shortly after. */ export function fetchWithTimeout(url: string, init: RequestInit = {}, timeoutMs: number): Promise { + let timeoutId: ReturnType; return Promise.race([ - fetch(url, init), - new Promise((_, reject) => - setTimeout(() => reject(new Error(`Request timed out after ${timeoutMs}ms`)), timeoutMs) - ), + fetch(url, init).then(response => { + clearTimeout(timeoutId); + return response; + }), + new Promise((_, reject) => { + timeoutId = setTimeout(() => reject(new Error(`Request timed out after ${timeoutMs}ms`)), timeoutMs); + }), ]); }