@@ -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 ) ;
@@ -174,6 +180,7 @@ Connection.prototype._handleFatalError = function(err) {
174
180
} ;
175
181
176
182
Connection . prototype . _handleNetworkError = function ( err ) {
183
+ console . log ( '_handleNetworkError AAAAA NETWORK ERROR' ) ;
177
184
this . _handleFatalError ( err ) ;
178
185
} ;
179
186
@@ -212,7 +219,16 @@ Connection.prototype._notifyError = function(err) {
212
219
connection . _command . onResult ( err ) ;
213
220
connection . _command = null ;
214
221
} else {
215
- bubbleErrorToConnection = true ;
222
+ // connection handshake is special because we allow it to be implicit
223
+ // if error happened during handshake, but there are others commands in queue
224
+ // then bubble error to other commands and not to connection
225
+ if (
226
+ ! ( connection . _command &&
227
+ connection . _command . constructor == Commands . ClientHandshake &&
228
+ connection . _commands . length > 0 )
229
+ ) {
230
+ bubbleErrorToConnection = true ;
231
+ }
216
232
}
217
233
while ( ( command = connection . _commands . shift ( ) ) ) {
218
234
if ( command . onResult ) {
@@ -900,7 +916,7 @@ Connection.prototype.writeOk = function(args) {
900
916
Connection . prototype . writeError = function ( args ) {
901
917
// if we want to send error before initial hello was sent, use default encoding
902
918
var encoding = this . serverConfig ? this . serverConfig . encoding : 'cesu8' ;
903
- this . writePacket ( Packets . Error . toPacket ( args , this . serverConfig . encoding ) ) ;
919
+ this . writePacket ( Packets . Error . toPacket ( args , encoding ) ) ;
904
920
} ;
905
921
906
922
Connection . prototype . serverHandshake = function serverHandshake ( args ) {
0 commit comments