Skip to content

Commit 7ae20e6

Browse files
authored
fix: bad client destroy on servername change (#3066)
1 parent ad3fac5 commit 7ae20e6

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lib/dispatcher/client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,10 @@ function _resume (client, sync) {
581581
}
582582

583583
client[kServerName] = request.servername
584-
client[kHTTPContext]?.destroy(new InformationalError('servername changed'))
584+
client[kHTTPContext]?.destroy(new InformationalError('servername changed'), () => {
585+
client[kHTTPContext] = null
586+
resume(client)
587+
})
585588
}
586589

587590
if (client[kConnecting]) {

test/node-test/client-dispatch.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { test } = require('node:test')
44
const assert = require('node:assert/strict')
55
const http = require('node:http')
6+
const https = require('node:https')
67
const { Client, Pool, errors } = require('../..')
78
const stream = require('node:stream')
89
const { createSecureServer } = require('node:http2')
@@ -959,3 +960,91 @@ test('dispatches in expected order for http2', async (t) => {
959960

960961
await p.completed
961962
})
963+
964+
test('Issue#3065 - fix bad destroy handling', async (t) => {
965+
const p = tspl(t, { plan: 4 })
966+
const server = https.createServer(pem, (req, res) => {
967+
res.writeHead(200, { 'content-type': 'text/plain' })
968+
res.end('ended')
969+
})
970+
971+
server.listen(0, () => {
972+
const client = new Client(`https://localhost:${server.address().port}`, {
973+
connect: {
974+
rejectUnauthorized: false
975+
}
976+
})
977+
978+
t.after(closeClientAndServerAsPromise(client, server))
979+
980+
const dispatches = []
981+
const dispatches2 = []
982+
983+
client.once('disconnect', (...args) => {
984+
const [,, err] = args
985+
p.strictEqual(err.code, 'UND_ERR_INFO')
986+
p.strictEqual(err.message, 'servername changed')
987+
})
988+
989+
client.dispatch({
990+
path: '/',
991+
method: 'POST',
992+
body: 'body'
993+
}, {
994+
onConnect () {
995+
dispatches.push('onConnect')
996+
},
997+
onBodySent () {
998+
dispatches.push('onBodySent')
999+
},
1000+
onResponseStarted () {
1001+
dispatches.push('onResponseStarted')
1002+
},
1003+
onHeaders () {
1004+
dispatches.push('onHeaders')
1005+
},
1006+
onData () {
1007+
dispatches.push('onData')
1008+
},
1009+
onComplete () {
1010+
dispatches.push('onComplete')
1011+
p.deepStrictEqual(dispatches, ['onConnect', 'onBodySent', 'onResponseStarted', 'onHeaders', 'onData', 'onComplete'])
1012+
},
1013+
onError (err) {
1014+
p.ifError(err)
1015+
}
1016+
})
1017+
1018+
client.dispatch({
1019+
servername: 'google.com',
1020+
path: '/',
1021+
method: 'POST',
1022+
body: 'body'
1023+
}, {
1024+
onConnect () {
1025+
dispatches2.push('onConnect')
1026+
},
1027+
onBodySent () {
1028+
dispatches2.push('onBodySent')
1029+
},
1030+
onResponseStarted () {
1031+
dispatches2.push('onResponseStarted')
1032+
},
1033+
onHeaders () {
1034+
dispatches2.push('onHeaders')
1035+
},
1036+
onData () {
1037+
dispatches2.push('onData')
1038+
},
1039+
onComplete () {
1040+
dispatches2.push('onComplete')
1041+
p.deepStrictEqual(dispatches2, ['onConnect', 'onBodySent', 'onResponseStarted', 'onHeaders', 'onData', 'onComplete'])
1042+
},
1043+
onError (err) {
1044+
p.ifError(err)
1045+
}
1046+
})
1047+
})
1048+
1049+
await p.completed
1050+
})

0 commit comments

Comments
 (0)