Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bad client destroy on servername change #3066

Merged
merged 2 commits into from
Apr 7, 2024
Merged
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
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
})
Loading