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
22 changes: 20 additions & 2 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const assert = require('node:assert')
const { getMaxListeners, setMaxListeners, defaultMaxListeners } = require('node:events')

const kAbortController = Symbol('abortController')
const kDependantAbortController = Symbol('dependantAbortController')

const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
signal.removeEventListener('abort', abort)
Expand Down Expand Up @@ -428,6 +429,12 @@ class Request {
// See, https://github.com/nodejs/undici/issues/1926.
this[kAbortController] = ac

// Keep ref to original ac while cloned request is alive.
// See, https://github.com/nodejs/undici/issues/4068.
if (webidl.is.Request(input) && input[kAbortController]) {
this[kDependantAbortController] = input[kAbortController]
}

const acRef = new WeakRef(ac)
const abort = buildAbort(acRef)

Expand Down Expand Up @@ -790,8 +797,19 @@ class Request {
)
}

// 4. Return clonedRequestObject.
return fromInnerRequest(clonedRequest, this.#dispatcher, ac.signal, getHeadersGuard(this.#headers))
// 4. Construct clonedRequestObject.
const req = fromInnerRequest(clonedRequest, this.#dispatcher, ac.signal, getHeadersGuard(this.#headers))
req[kAbortController] = ac

// 5. Keep reference of original controller to avoid GC
// Keep ref to original ac while cloned request is alive.
// See, https://github.com/nodejs/undici/issues/4068.
if (this[kAbortController]) {
req[kDependantAbortController] = this[kAbortController]
}

// 6. Return cloned request
return req
}

[nodeUtil.inspect.custom] (depth, options) {
Expand Down
60 changes: 60 additions & 0 deletions test/fetch/issue-4068.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

const { once } = require('node:events')
const { test, describe } = require('node:test')
const { fetch, Request } = require('../..')
const { createServer } = require('node:http')

describe('issue 4068', async () => {
test('abort signal for new request', async (t) => {
t.plan(2)
const server = createServer(() => {}).listen(0)
let aborted = false

t.after(server.close.bind(server))

const ac = new AbortController()
let req = new Request(`http://localhost:${server.address().port}`, {
signal: ac.signal
})

req.signal.addEventListener('abort', () => {
aborted = true
})

req = new Request(req)
setTimeout(() => {
global.gc()
ac.abort()
})
await once(server, 'listening')
await t.assert.rejects(fetch(req))
t.assert.ok(aborted)
})

test('abort signal for cloned request', async (t) => {
t.plan(2)
const server = createServer(() => {}).listen(0)
let aborted = false

t.after(server.close.bind(server))

const ac = new AbortController()
let req = new Request(`http://localhost:${server.address().port}`, {
signal: ac.signal
})

req.signal.addEventListener('abort', () => {
aborted = true
})

req = req.clone()
setTimeout(() => {
global.gc()
ac.abort()
})
await once(server, 'listening')
await t.assert.rejects(fetch(req))
t.assert.ok(aborted)
})
})
Loading