From f729f1c9883f79c3ec53795b41e3316c0cb57d8c Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 30 Sep 2024 16:27:52 +0200 Subject: [PATCH] Fix race condition around timeou of slow bodies Closes remarkjs/remark-lint-no-dead-urls#53. --- lib/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 92d71fb..02c6386 100644 --- a/lib/index.js +++ b/lib/index.js @@ -195,6 +195,14 @@ async function deadOrAliveInternal(state, url) { let response try { + // Create a manually abortable fetch, + // instead of `AbortSignal.timeout(state.timeout)`. + // This way we only abort slow requests; not the other work. + const controller = new AbortController() + const id = setTimeout(function () { + controller.abort() + }, state.timeout) + response = await fetch(url, { headers: { userAgent: state.userAgent, @@ -207,8 +215,10 @@ async function deadOrAliveInternal(state, url) { }, method: 'GET', redirect: 'manual', - signal: AbortSignal.timeout(state.timeout) + signal: controller.signal }) + + clearTimeout(id) } catch (error) { if (state.retries < state.maxRetries) return retry(state, url)