fix(#3736): back-port 183f8e9 to v6.x (#3855) · nodejs/undici@353ab63 (original) (raw)

Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ class RequestHandler extends AsyncResource {
73 73 this.removeAbortListener = util.addAbortListener(this.signal, () => {
74 74 this.reason = this.signal.reason ?? new RequestAbortedError()
75 75 if (this.res) {
76 -util.destroy(this.res, this.reason)
76 +util.destroy(this.res.on('error', util.nop), this.reason)
77 77 } else if (this.abort) {
78 78 this.abort(this.reason)
79 79 }
Original file line number Diff line number Diff line change
@@ -1252,3 +1252,39 @@ test('request post body DataView', async (t) => {
1252 1252
1253 1253 await t.completed
1254 1254 })
1255 +
1256 +test('#3736 - Aborted Response (without consuming body)', async (t) => {
1257 +const plan = tspl(t, { plan: 1 })
1258 +
1259 +const controller = new AbortController()
1260 +const server = createServer((req, res) => {
1261 +setTimeout(() => {
1262 +res.writeHead(200, 'ok', {
1263 +'content-type': 'text/plain'
1264 +})
1265 +res.write('hello from server')
1266 +res.end()
1267 +}, 100)
1268 +})
1269 +
1270 +server.listen(0)
1271 +
1272 +await EE.once(server, 'listening')
1273 +const client = new Client(`http://localhost:${server.address().port}\`)
1274 +
1275 +after(server.close.bind(server))
1276 +after(client.destroy.bind(client))
1277 +
1278 +const { signal } = controller
1279 +const promise = client.request({
1280 +path: '/',
1281 +method: 'GET',
1282 + signal
1283 +})
1284 +
1285 +controller.abort()
1286 +
1287 +await plan.rejects(promise, { message: 'This operation was aborted' })
1288 +
1289 +await plan.completed
1290 +})
Original file line number Diff line number Diff line change
@@ -334,23 +334,15 @@ test(
334 334
335 335 after(() => server.close())
336 336 after(() => client.close())
337 -t = tspl(t, { plan: 2 })
337 +t = tspl(t, { plan: 1 })
338 338
339 -try {
340 -await client.request({
341 -path: '/',
342 -method: 'GET',
343 -headers: {
344 -'x-my-header': 'foo'
345 -}
346 -})
347 -} catch (error) {
348 -t.strictEqual(
349 -error.message,
350 -'Client network socket disconnected before secure TLS connection was established'
351 -)
352 -t.strictEqual(error.code, 'ECONNRESET')
353 -}
339 +await t.rejects(client.request({
340 +path: '/',
341 +method: 'GET',
342 +headers: {
343 +'x-my-header': 'foo'
344 +}
345 +}))
354 346 }
355 347 )
356 348