@@ -87,7 +87,7 @@ function Connection(opts) {
87
87
88
88
this . clientEncoding = CharsetToEncoding [ this . config . charsetNumber ] ;
89
89
90
- this . stream . once ( 'error' , connection . _handleNetworkError . bind ( this ) ) ;
90
+ this . stream . on ( 'error' , connection . _handleNetworkError . bind ( this ) ) ;
91
91
92
92
// see https://gist.github.com/khoomeister/4985691#use-that-instead-of-bind
93
93
this . packetParser = new PacketParser ( function ( p ) {
@@ -123,11 +123,17 @@ function Connection(opts) {
123
123
if ( ! this . config . isServer ) {
124
124
handshakeCommand = new Commands . ClientHandshake ( this . config . clientFlags ) ;
125
125
handshakeCommand . on ( 'end' , function ( ) {
126
+ // this happens when handshake finishes early and first packet is error
127
+ // and not server hello ( for example, 'Too many connactions' error)
128
+ if ( ! handshakeCommand . handshake ) {
129
+ return ;
130
+ }
126
131
connection . _handshakePacket = handshakeCommand . handshake ;
127
132
connection . threadId = handshakeCommand . handshake . connectionId ;
128
133
connection . emit ( 'connect' , handshakeCommand . handshake ) ;
129
134
} ) ;
130
135
handshakeCommand . on ( 'error' , function ( err ) {
136
+ connection . _closing = true ;
131
137
connection . _notifyError ( err ) ;
132
138
} ) ;
133
139
this . addCommand ( handshakeCommand ) ;
@@ -212,7 +218,16 @@ Connection.prototype._notifyError = function(err) {
212
218
connection . _command . onResult ( err ) ;
213
219
connection . _command = null ;
214
220
} else {
215
- bubbleErrorToConnection = true ;
221
+ // connection handshake is special because we allow it to be implicit
222
+ // if error happened during handshake, but there are others commands in queue
223
+ // then bubble error to other commands and not to connection
224
+ if (
225
+ ! ( connection . _command &&
226
+ connection . _command . constructor == Commands . ClientHandshake &&
227
+ connection . _commands . length > 0 )
228
+ ) {
229
+ bubbleErrorToConnection = true ;
230
+ }
216
231
}
217
232
while ( ( command = connection . _commands . shift ( ) ) ) {
218
233
if ( command . onResult ) {
@@ -791,6 +806,10 @@ Connection.prototype.destroy = function() {
791
806
} ;
792
807
793
808
Connection . prototype . close = function ( ) {
809
+ if ( this . connectTimeout ) {
810
+ Timers . clearTimeout ( this . connectTimeout ) ;
811
+ this . connectTimeout = null ;
812
+ }
794
813
this . _closing = true ;
795
814
this . stream . end ( ) ;
796
815
var connection = this ;
@@ -900,7 +919,7 @@ Connection.prototype.writeOk = function(args) {
900
919
Connection . prototype . writeError = function ( args ) {
901
920
// if we want to send error before initial hello was sent, use default encoding
902
921
var encoding = this . serverConfig ? this . serverConfig . encoding : 'cesu8' ;
903
- this . writePacket ( Packets . Error . toPacket ( args , this . serverConfig . encoding ) ) ;
922
+ this . writePacket ( Packets . Error . toPacket ( args , encoding ) ) ;
904
923
} ;
905
924
906
925
Connection . prototype . serverHandshake = function serverHandshake ( args ) {
0 commit comments