From 3bce7395c805fe93447eba9fa033a41874f6e6e1 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 12 Apr 2023 08:56:31 +0200 Subject: [PATCH] fix: don't leak socket if client is destroyed will connecting --- lib/client.js | 12 +++++++++++- test/connect-abort.js | 28 ++++++++++++++++++++++++++++ types/connector.d.ts | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/connect-abort.js diff --git a/lib/client.js b/lib/client.js index 370e89b89cf..e630212fa79 100644 --- a/lib/client.js +++ b/lib/client.js @@ -21,7 +21,8 @@ const { InformationalError, BodyTimeoutError, HTTPParserError, - ResponseExceededMaxSizeError + ResponseExceededMaxSizeError, + ClientDestroyedError } = require('./core/errors') const buildConnector = require('./core/connect') const { @@ -1083,6 +1084,11 @@ async function connect (client) { }) }) + if (client.destroyed) { + util.destroy(socket.on('error', () => {}), new ClientDestroyedError()) + return + } + if (!llhttpInstance) { llhttpInstance = await llhttpPromise llhttpPromise = null @@ -1125,6 +1131,10 @@ async function connect (client) { } client.emit('connect', client[kUrl], [client]) } catch (err) { + if (client.destroyed) { + return + } + client[kConnecting] = false if (channels.connectError.hasSubscribers) { diff --git a/test/connect-abort.js b/test/connect-abort.js new file mode 100644 index 00000000000..6eb36243866 --- /dev/null +++ b/test/connect-abort.js @@ -0,0 +1,28 @@ +'use strict' + +const { test } = require('tap') +const { Client } = require('..') +const { PassThrough } = require('stream') + +test(t => { + t.plan(2) + + const client = new Client('http://localhost:1234', { + connect: (_, cb) => { + client.destroy() + cb(null, new PassThrough({ + destroy (err, cb) { + t.same(err?.name, 'ClientDestroyedError') + cb(null) + } + })) + } + }) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.same(err?.name, 'ClientDestroyedError') + }) +}) diff --git a/types/connector.d.ts b/types/connector.d.ts index 847284a1f2b..b85e6b81585 100644 --- a/types/connector.d.ts +++ b/types/connector.d.ts @@ -21,7 +21,7 @@ declare namespace buildConnector { port: string servername?: string localAddress?: string | null - httpSocket?: Socket + httpSocket?: Socket, } export type Callback = (...args: CallbackArgs) => void