Skip to content

Commit a5eac88

Browse files
authored
fix: post request signal (#3354)
1 parent ce240da commit a5eac88

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

lib/api/api-request.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class RequestHandler extends AsyncResource {
154154
}
155155

156156
onComplete (trailers) {
157+
if (this.removeAbortListener) {
158+
this.removeAbortListener()
159+
this.removeAbortListener = null
160+
}
161+
157162
util.parseHeaders(trailers, this.trailers)
158163
this.res.push(null)
159164
}

test/request-signal.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ test('post abort signal', async (t) => {
3636

3737
server.listen(0, async () => {
3838
const ac = new AbortController()
39-
const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
39+
const uresPromise = request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
4040
ac.abort()
41+
4142
try {
43+
const ures = await uresPromise
4244
/* eslint-disable-next-line no-unused-vars */
4345
for await (const chunk of ures.body) {
4446
// Do nothing...
@@ -61,9 +63,11 @@ test('post abort signal w/ reason', async (t) => {
6163
server.listen(0, async () => {
6264
const ac = new AbortController()
6365
const _err = new Error()
64-
const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
66+
const uresPromise = request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
6567
ac.abort(_err)
68+
6669
try {
70+
const ures = await uresPromise
6771
/* eslint-disable-next-line no-unused-vars */
6872
for await (const chunk of ures.body) {
6973
// Do nothing...
@@ -74,3 +78,20 @@ test('post abort signal w/ reason', async (t) => {
7478
})
7579
await t.completed
7680
})
81+
82+
test('post abort signal after request completed', async (t) => {
83+
t = tspl(t, { plan: 1 })
84+
85+
const server = createServer((req, res) => {
86+
res.end('asd')
87+
})
88+
after(() => server.close())
89+
90+
server.listen(0, async () => {
91+
const ac = new AbortController()
92+
const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal })
93+
ac.abort()
94+
t.equal(await ures.body.text(), 'asd')
95+
})
96+
await t.completed
97+
})

0 commit comments

Comments
 (0)