Skip to content

Commit

Permalink
fix(H2-#3140): abort requets upon GOAWAY (#3143)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Apr 21, 2024
1 parent 9547fe4 commit b07a9d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 2 additions & 4 deletions lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ function onHttp2SessionEnd () {
* This is the root cause of #3011
* We need to handle GOAWAY frames properly, and trigger the session close
* along with the socket right away
* Find a way to trigger the close cycle from here on.
*/
function onHTTP2GoAway (code) {
const err = new InformationalError(`HTTP/2: "GOAWAY" frame received with code ${code}`)
const err = new RequestAbortedError(`HTTP/2: "GOAWAY" frame received with code ${code}`)

// We need to trigger the close cycle right away
// We need to destroy the session and the socket
Expand All @@ -220,8 +219,7 @@ function onHTTP2GoAway (code) {
this[kClient][kOnError](err)

this.unref()
// We send the GOAWAY frame response as no error
this.destroy()

util.destroy(this[kSocket], err)
}

Expand Down
1 change: 1 addition & 0 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ function onError (client, err) {
assert(client[kPendingIdx] === client[kRunningIdx])

const requests = client[kQueue].splice(client[kRunningIdx])

for (let i = 0; i < requests.length; i++) {
const request = requests[i]
util.errorRequest(client, request, err)
Expand Down
23 changes: 12 additions & 11 deletions test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ test('Should support H2 GOAWAY (server-side)', async t => {

server.on('session', session => {
setTimeout(() => {
session.goaway(204)
session.goaway(0)
}, 1000)
})

Expand Down Expand Up @@ -274,7 +274,7 @@ test('Should support H2 GOAWAY (server-side)', async t => {

t.ok(url instanceof URL)
t.deepStrictEqual(disconnectClient, [client])
t.strictEqual(err.message, 'HTTP/2: "GOAWAY" frame received with code 204')
t.strictEqual(err.message, 'HTTP/2: "GOAWAY" frame received with code 0')
})

test('Should throw if bad allowH2 has been passed', async t => {
Expand Down Expand Up @@ -1427,20 +1427,21 @@ test('#3046 - GOAWAY Frame', async t => {
t.strictEqual(err.message, 'HTTP/2: "GOAWAY" frame received with code 0')
})

client.request({
const response = await client.request({
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
}, (err, response) => {
t.ifError(err)
t.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
t.strictEqual(response.headers['x-custom-h2'], 'hello')
t.strictEqual(response.statusCode, 200)
// We stop the sent the GOAWAY frame before the body is sent, as we received the GOAWAY frame
// before the DATA one, the body will be empty
response.body.dump()
})

t.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
t.strictEqual(response.headers['x-custom-h2'], 'hello')
t.strictEqual(response.statusCode, 200)

t.rejects(response.body.text.bind(response.body), {
message: 'HTTP/2: "GOAWAY" frame received with code 0',
code: 'UND_ERR_ABORTED'
})

await t.completed
Expand Down

0 comments on commit b07a9d2

Please sign in to comment.