Skip to content

Commit

Permalink
Fix race condition around timeou of slow bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 30, 2024
1 parent 42d293a commit f729f1c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down

0 comments on commit f729f1c

Please sign in to comment.