Skip to content

Commit

Permalink
fix: don't leak socket listener (#431)
Browse files Browse the repository at this point in the history
Fixes: #430
  • Loading branch information
ronag committed Sep 23, 2020
1 parent 4f68bf8 commit 3d7c46e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
6 changes: 0 additions & 6 deletions lib/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,6 @@ function write (client, request) {
const onAbort = function () {
onFinished(new RequestAbortedError())
}
/* istanbul ignore next */
const onClose = function () {
assert(false, 'socket should not close without error')
}
const onFinished = function (err) {
if (finished) {
return
Expand All @@ -1123,7 +1119,6 @@ function write (client, request) {
socket
.removeListener('drain', onDrain)
.removeListener('error', onFinished)
.removeListener('close', onClose)
body
.removeListener('data', onData)
.removeListener('end', onFinished)
Expand Down Expand Up @@ -1165,7 +1160,6 @@ function write (client, request) {
socket
.on('drain', onDrain)
.on('error', onFinished)
.on('close', onFinished)
}

return true
Expand Down
49 changes: 49 additions & 0 deletions test/client-write-max-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

const { test } = require('tap')
const { Client } = require('..')
const { createServer } = require('http')
const { Readable } = require('stream')

test('socket close listener does not leak', (t) => {
t.plan(32)

const server = createServer()

server.on('request', (req, res) => {
res.end('hello')
})
t.tearDown(server.close.bind(server))

const makeBody = () => {
return new Readable({
read () {
process.nextTick(() => {
this.push(null)
})
}
})
}

const onRequest = (err, data) => {
t.error(err)
data.body.on('end', () => t.pass()).resume()
}

process.on('warning', () => {
t.fail()
})

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.tearDown(client.destroy.bind(client))

client.on('disconnect', () => {
t.fail()
})

for (let n = 0; n < 16; ++n) {
client.request({ path: '/', method: 'GET', body: makeBody() }, onRequest)
}
})
})

0 comments on commit 3d7c46e

Please # to comment.