Skip to content

Commit

Permalink
fix: post request signal (#3354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigioliva authored Jun 22, 2024
1 parent ce240da commit a5eac88
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class RequestHandler extends AsyncResource {
}

onComplete (trailers) {
if (this.removeAbortListener) {
this.removeAbortListener()
this.removeAbortListener = null
}

util.parseHeaders(trailers, this.trailers)
this.res.push(null)
}
Expand Down
25 changes: 23 additions & 2 deletions test/request-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ test('post abort signal', async (t) => {

server.listen(0, async () => {
const ac = new AbortController()
const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
const uresPromise = request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
ac.abort()

try {
const ures = await uresPromise
/* eslint-disable-next-line no-unused-vars */
for await (const chunk of ures.body) {
// Do nothing...
Expand All @@ -61,9 +63,11 @@ test('post abort signal w/ reason', async (t) => {
server.listen(0, async () => {
const ac = new AbortController()
const _err = new Error()
const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
const uresPromise = request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
ac.abort(_err)

try {
const ures = await uresPromise
/* eslint-disable-next-line no-unused-vars */
for await (const chunk of ures.body) {
// Do nothing...
Expand All @@ -74,3 +78,20 @@ test('post abort signal w/ reason', async (t) => {
})
await t.completed
})

test('post abort signal after request completed', async (t) => {
t = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
res.end('asd')
})
after(() => server.close())

server.listen(0, async () => {
const ac = new AbortController()
const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
ac.abort()
t.equal(await ures.body.text(), 'asd')
})
await t.completed
})

1 comment on commit a5eac88

@ronag
Copy link
Member

@ronag ronag commented on a5eac88 Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests were correct. This breaks stuff.

Please sign in to comment.