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

Commit

Permalink
fix(connection): do not leak a connection if initial handshak fails
Browse files Browse the repository at this point in the history
Fixes NODE-1909
  • Loading branch information
daprahamian authored and mbroadst committed Mar 22, 2019
1 parent 72bb011 commit 6cba222
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/connection/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ function checkSupportedServer(ismaster, options) {
return new MongoError(message);
}

function performInitialHandshake(conn, options, callback) {
function performInitialHandshake(conn, options, _callback) {
const callback = function(err, ret) {
if (err && conn) {
conn.destroy();
}
_callback(err, ret);
};

let compressors = [];
if (options.compression && options.compression.compressors) {
compressors = options.compression.compressors;
Expand Down Expand Up @@ -229,7 +236,7 @@ function parseSslOptions(family, options) {
return result;
}

function makeConnection(family, options, callback) {
function makeConnection(family, options, _callback) {
const useSsl = typeof options.ssl === 'boolean' ? options.ssl : false;
const keepAlive = typeof options.keepAlive === 'boolean' ? options.keepAlive : true;
let keepAliveInitialDelay =
Expand All @@ -246,6 +253,13 @@ function makeConnection(family, options, callback) {
}

let socket;
const callback = function(err, ret) {
if (err && socket) {
socket.destroy();
}
_callback(err, ret);
};

try {
if (useSsl) {
socket = tls.connect(parseSslOptions(family, options));
Expand Down

0 comments on commit 6cba222

Please sign in to comment.