Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/handler/deduplication-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,22 @@ class DeduplicationHandler {
get aborted () { return state.aborted },
get reason () { return state.reason },
abort: (reason) => {
if (state.aborted) {
return
}

state.aborted = true
state.reason = reason ?? null
waitingHandler.done = true
waitingHandler.pendingTrailers = null
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0

try {
handler.onResponseError?.(waitingHandler.controller, state.reason ?? new RequestAbortedError())
} catch {
// Ignore errors from waiting handlers
}
}
}

Expand Down Expand Up @@ -444,12 +454,8 @@ class DeduplicationHandler {
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0

try {
waitingHandler.controller.abort(err)
waitingHandler.handler.onResponseError?.(waitingHandler.controller, err)
} catch {
// Ignore errors from waiting handlers
}
// controller.abort(err) notifies the handler via onResponseError
waitingHandler.controller.abort(err)
}

#pruneDoneWaitingHandlers () {
Expand Down
69 changes: 69 additions & 0 deletions test/interceptors/deduplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,4 +1375,73 @@ describe('Deduplicate Interceptor', () => {

strictEqual(key1, key2)
})

test('aborting a deduplicated request settles it with an AbortError', async () => {
const server = createServer(async (req, res) => {
await sleep(100)
res.end('response-body')
}).listen(0)

const client = new Client(`http://localhost:${server.address().port}`)
.compose(interceptors.deduplicate())

after(async () => {
server.close()
await client.close()
})

await once(server, 'listening')

const abortController = new AbortController()

const primary = client.request({ origin: 'localhost', method: 'GET', path: '/' })
const deduplicated = client.request({ origin: 'localhost', method: 'GET', path: '/', signal: abortController.signal })

abortController.abort()

// The deduplicated request must settle; without a fix it never does
const outcome = await Promise.race([
deduplicated.then(() => 'resolved', err => err),
sleep(1000).then(() => 'timed-out')
])
strictEqual(outcome?.name, 'AbortError')

// The primary request is unaffected
const res = await primary
strictEqual(await res.body.text(), 'response-body')
})

test('a deduplicated request with an already aborted signal rejects with an AbortError', async () => {
const server = createServer(async (req, res) => {
await sleep(100)
res.end('response-body')
}).listen(0)

const client = new Client(`http://localhost:${server.address().port}`)
.compose(interceptors.deduplicate())

after(async () => {
server.close()
await client.close()
})

await once(server, 'listening')

const abortController = new AbortController()
abortController.abort()

const primary = client.request({ origin: 'localhost', method: 'GET', path: '/' })
const deduplicated = client.request({ origin: 'localhost', method: 'GET', path: '/', signal: abortController.signal })

// The deduplicated request must settle; without a fix it never does
const outcome = await Promise.race([
deduplicated.then(() => 'resolved', err => err),
sleep(1000).then(() => 'timed-out')
])
strictEqual(outcome?.name, 'AbortError')

// The primary request is unaffected
const res = await primary
strictEqual(await res.body.text(), 'response-body')
})
})
Loading