Skip to content

Commit

Permalink
fix: bad client destroy on servername change (#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Apr 7, 2024
1 parent ad3fac5 commit 7ae20e6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,10 @@ function _resume (client, sync) {
}

client[kServerName] = request.servername
client[kHTTPContext]?.destroy(new InformationalError('servername changed'))
client[kHTTPContext]?.destroy(new InformationalError('servername changed'), () => {
client[kHTTPContext] = null
resume(client)
})
}

if (client[kConnecting]) {
Expand Down
89 changes: 89 additions & 0 deletions test/node-test/client-dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { test } = require('node:test')
const assert = require('node:assert/strict')
const http = require('node:http')
const https = require('node:https')
const { Client, Pool, errors } = require('../..')
const stream = require('node:stream')
const { createSecureServer } = require('node:http2')
Expand Down Expand Up @@ -959,3 +960,91 @@ test('dispatches in expected order for http2', async (t) => {

await p.completed
})

test('Issue#3065 - fix bad destroy handling', async (t) => {
const p = tspl(t, { plan: 4 })
const server = https.createServer(pem, (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('ended')
})

server.listen(0, () => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
})

t.after(closeClientAndServerAsPromise(client, server))

const dispatches = []
const dispatches2 = []

client.once('disconnect', (...args) => {
const [,, err] = args
p.strictEqual(err.code, 'UND_ERR_INFO')
p.strictEqual(err.message, 'servername changed')
})

client.dispatch({
path: '/',
method: 'POST',
body: 'body'
}, {
onConnect () {
dispatches.push('onConnect')
},
onBodySent () {
dispatches.push('onBodySent')
},
onResponseStarted () {
dispatches.push('onResponseStarted')
},
onHeaders () {
dispatches.push('onHeaders')
},
onData () {
dispatches.push('onData')
},
onComplete () {
dispatches.push('onComplete')
p.deepStrictEqual(dispatches, ['onConnect', 'onBodySent', 'onResponseStarted', 'onHeaders', 'onData', 'onComplete'])
},
onError (err) {
p.ifError(err)
}
})

client.dispatch({
servername: 'google.com',
path: '/',
method: 'POST',
body: 'body'
}, {
onConnect () {
dispatches2.push('onConnect')
},
onBodySent () {
dispatches2.push('onBodySent')
},
onResponseStarted () {
dispatches2.push('onResponseStarted')
},
onHeaders () {
dispatches2.push('onHeaders')
},
onData () {
dispatches2.push('onData')
},
onComplete () {
dispatches2.push('onComplete')
p.deepStrictEqual(dispatches2, ['onConnect', 'onBodySent', 'onResponseStarted', 'onHeaders', 'onData', 'onComplete'])
},
onError (err) {
p.ifError(err)
}
})
})

await p.completed
})

0 comments on commit 7ae20e6

Please sign in to comment.