Skip to content

Commit f5135f3

Browse files
committed
fix
1 parent dc9c152 commit f5135f3

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

lib/Connection.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ function Connection(options) {
1515

1616
this.config = options.config;
1717

18-
this._socket = options.socket;
18+
this._socket = null;
1919
this._protocol = new Protocol({config: this.config, connection: this});
20+
this._protocolSetup = false;
2021
this._connectCalled = false;
2122
this.state = 'disconnected';
2223
this.threadId = null;
@@ -67,19 +68,29 @@ Connection.prototype.connect = function connect(options, callback) {
6768
options = {};
6869
}
6970

71+
this._setupProtocolHandlers();
72+
this._protocol.handshake(options, wrapCallbackInDomain(this, callback));
73+
7074
if (!this._connectCalled) {
7175
this._connectCalled = true;
76+
this._socket = null;
7277

73-
// Connect either via a UNIX domain socket or a TCP socket.
74-
this._socket = (this.config.socketPath)
75-
? Net.createConnection(this.config.socketPath)
76-
: Net.createConnection(this.config.port, this.config.host);
78+
try {
79+
// Connect either via a UNIX domain socket or a TCP socket.
80+
this._socket = (this.config.socketPath)
81+
? Net.createConnection(this.config.socketPath)
82+
: Net.createConnection(this.config.port, this.config.host);
83+
} catch (err) {
84+
this._handleNetworkError(err);
85+
return;
86+
}
7787

7888
// Connect socket to connection domain
7989
if (Events.usingDomains) {
8090
this._socket.domain = this.domain;
8191
}
8292

93+
// socket <-> protocol
8394
var connection = this;
8495
this._protocol.on('data', function(data) {
8596
connection._socket.write(data);
@@ -94,15 +105,11 @@ Connection.prototype.connect = function connect(options, callback) {
94105
connection._protocol.end();
95106
}));
96107

108+
// Set up socket handlers
97109
this._socket.on('error', this._handleNetworkError.bind(this));
98110
this._socket.on('connect', this._handleProtocolConnect.bind(this));
99-
this._protocol.on('handshake', this._handleProtocolHandshake.bind(this));
100-
this._protocol.on('initialize', this._handleProtocolInitialize.bind(this));
101-
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
102-
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
103-
this._protocol.on('end', this._handleProtocolEnd.bind(this));
104-
this._protocol.on('enqueue', this._handleProtocolEnqueue.bind(this));
105111

112+
// Set connect timeout
106113
if (this.config.connectTimeout) {
107114
var handleConnectTimeout = this._handleConnectTimeout.bind(this);
108115

@@ -112,8 +119,6 @@ Connection.prototype.connect = function connect(options, callback) {
112119
});
113120
}
114121
}
115-
116-
this._protocol.handshake(options, wrapCallbackInDomain(this, callback));
117122
};
118123

119124
Connection.prototype.changeUser = function changeUser(options, callback) {
@@ -241,18 +246,28 @@ Connection.prototype.end = function end(options, callback) {
241246

242247
Connection.prototype.destroy = function() {
243248
this.state = 'disconnected';
244-
this._implyConnect();
245-
this._socket.destroy();
249+
this._setupProtocolHandlers();
250+
251+
if (this._socket) {
252+
this._socket.destroy();
253+
}
254+
246255
this._protocol.destroy();
247256
};
248257

249258
Connection.prototype.pause = function() {
250-
this._socket.pause();
259+
if (this._socket) {
260+
this._socket.pause();
261+
}
262+
251263
this._protocol.pause();
252264
};
253265

254266
Connection.prototype.resume = function() {
255-
this._socket.resume();
267+
if (this._socket) {
268+
this._socket.resume();
269+
}
270+
256271
this._protocol.resume();
257272
};
258273

@@ -450,11 +465,25 @@ Connection.prototype._handleProtocolEnqueue = function _handleProtocolEnqueue(se
450465
};
451466

452467
Connection.prototype._implyConnect = function() {
453-
if (!this._connectCalled) {
468+
this._setupProtocolHandlers();
469+
470+
if (!this._connectCalled && !this._protocol._destroyed) {
454471
this.connect();
455472
}
456473
};
457474

475+
Connection.prototype._setupProtocolHandlers = function _setupProtocolHandlers() {
476+
if (!this._protocolSetup) {
477+
this._protocolSetup = true;
478+
this._protocol.on('handshake', this._handleProtocolHandshake.bind(this));
479+
this._protocol.on('initialize', this._handleProtocolInitialize.bind(this));
480+
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
481+
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
482+
this._protocol.on('end', this._handleProtocolEnd.bind(this));
483+
this._protocol.on('enqueue', this._handleProtocolEnqueue.bind(this));
484+
}
485+
};
486+
458487
function createSecureContext (config, cb) {
459488
var context = null;
460489
var error = null;

lib/protocol/Protocol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Protocol.prototype._enqueue = function(sequence) {
186186
sequence.on('end', function () {
187187
self._handshaked = true;
188188

189-
if (!self._fatalError) {
189+
if (!self._fatalError && !self._destroyed) {
190190
self.emit('handshake', self._handshakeInitializationPacket);
191191
}
192192
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
4+
var connection = common.createConnection({
5+
port: 99999
6+
});
7+
8+
connection.connect(function (err) {
9+
assert.ok(err);
10+
assert.strictEqual(err.fatal, true);
11+
assert.ok(
12+
err.code === 'ECONNREFUSED' /* Node.js < 0.12 */ ||
13+
err.code === 'ERR_SOCKET_BAD_PORT' /* Node.js > 8 */ ||
14+
err.name === 'RangeError'
15+
);
16+
connection.destroy();
17+
});

0 commit comments

Comments
 (0)