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
11 changes: 11 additions & 0 deletions lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const {
HTTP2_HEADER_EXPECT,
HTTP2_HEADER_STATUS,
HTTP2_HEADER_PROTOCOL,
NGHTTP2_CANCEL,
NGHTTP2_NO_ERROR,
NGHTTP2_REFUSED_STREAM
}
Expand Down Expand Up @@ -321,6 +322,7 @@ function connectH2 (client, socket) {
util.addListener(session, 'goaway', onHttp2SessionGoAway)
util.addListener(session, 'close', onHttp2SessionClose)
util.addListener(session, 'remoteSettings', onHttp2RemoteSettings)
util.addListener(session, 'stream', onHttp2SessionOrphanStream)
// TODO (@metcoder95): implement SETTINGS support
// util.addListener(session, 'localSettings', onHttp2RemoteSettings)

Expand Down Expand Up @@ -779,6 +781,15 @@ function onHttp2SessionGoAway (errorCode, lastStreamID) {
client[kResume]()
}

// Push is disabled, so a client session only emits 'stream' when the peer sends
// frames for a stream whose JS object undici already destroyed (e.g. an aborted
// request). Node then wraps the native stream in a new object with no 'error'
// listener, and a later session teardown with an error would crash the process.
function onHttp2SessionOrphanStream (stream) {
stream.on('error', noop)
closeStream(stream, NGHTTP2_CANCEL)
}

function onHttp2SessionClose () {
const { [kClient]: client, [kHTTP2SessionState]: state, [kSocket]: socket } = this

Expand Down
47 changes: 47 additions & 0 deletions test/issue-6143.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const assert = require('node:assert')
const { once } = require('node:events')
const { createServer } = require('node:http2')
const { test } = require('node:test')
const { setTimeout: sleep } = require('node:timers/promises')

const { Client, fetch } = require('..')

test('Issue #6143 - aborted fetch on an established h2 session does not cause an uncaught idle timeout', { timeout: 5000 }, async (t) => {
const server = createServer()
server.on('session', (session) => session.on('error', () => {}))
server.on('stream', (stream) => {
stream.on('error', () => {})
stream.respond({ ':status': 200 })
stream.end('ok')
})

server.listen(0, '127.0.0.1')
await once(server, 'listening')

const url = `http://127.0.0.1:${server.address().port}`
const client = new Client(url, {
useH2c: true,
keepAliveTimeout: 100
})

t.after(async () => {
await client.destroy()
await new Promise((resolve) => server.close(resolve))
})

await (await fetch(url, { dispatcher: client })).text()

const controller = new AbortController()
const request = fetch(url, {
dispatcher: client,
signal: controller.signal
})
controller.abort()

await assert.rejects(request, { name: 'AbortError' })

// Let the idle timer fire and tear the session down.
await sleep(300)
})