Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 6cba222

Browse files
daprahamianmbroadst
authored andcommitted
fix(connection): do not leak a connection if initial handshak fails
Fixes NODE-1909
1 parent 72bb011 commit 6cba222

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/connection/connect.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ function checkSupportedServer(ismaster, options) {
100100
return new MongoError(message);
101101
}
102102

103-
function performInitialHandshake(conn, options, callback) {
103+
function performInitialHandshake(conn, options, _callback) {
104+
const callback = function(err, ret) {
105+
if (err && conn) {
106+
conn.destroy();
107+
}
108+
_callback(err, ret);
109+
};
110+
104111
let compressors = [];
105112
if (options.compression && options.compression.compressors) {
106113
compressors = options.compression.compressors;
@@ -229,7 +236,7 @@ function parseSslOptions(family, options) {
229236
return result;
230237
}
231238

232-
function makeConnection(family, options, callback) {
239+
function makeConnection(family, options, _callback) {
233240
const useSsl = typeof options.ssl === 'boolean' ? options.ssl : false;
234241
const keepAlive = typeof options.keepAlive === 'boolean' ? options.keepAlive : true;
235242
let keepAliveInitialDelay =
@@ -246,6 +253,13 @@ function makeConnection(family, options, callback) {
246253
}
247254

248255
let socket;
256+
const callback = function(err, ret) {
257+
if (err && socket) {
258+
socket.destroy();
259+
}
260+
_callback(err, ret);
261+
};
262+
249263
try {
250264
if (useSsl) {
251265
socket = tls.connect(parseSslOptions(family, options));

0 commit comments

Comments
 (0)