@@ -179,8 +179,15 @@ C.open = function(allFields, openCallback0) {
179
179
function send ( Method ) {
180
180
// This can throw an exception if there's some problem with the
181
181
// options; e.g., something is a string instead of a number.
182
- try { self . sendMethod ( 0 , Method , tunedOptions ) ; }
183
- catch ( err ) { bail ( err ) ; }
182
+ try {
183
+ self . sendMethod ( 0 , Method , tunedOptions ) ;
184
+ } catch ( err ) {
185
+ bail ( err ) ;
186
+
187
+ // We are in an inconsistent state so we can't continue,
188
+ // rethrowing the exception will stop further execution.
189
+ throw err ;
190
+ }
184
191
}
185
192
186
193
function negotiate ( server , desired ) {
@@ -206,8 +213,13 @@ C.open = function(allFields, openCallback0) {
206
213
return ;
207
214
}
208
215
self . serverProperties = start . fields . serverProperties ;
209
- send ( defs . ConnectionStartOk ) ;
210
- wait ( afterStartOk ) ;
216
+ try {
217
+ send ( defs . ConnectionStartOk ) ;
218
+ wait ( afterStartOk ) ;
219
+ } catch {
220
+ // Exit, callback with error already called,
221
+ return ;
222
+ }
211
223
}
212
224
213
225
function afterStartOk ( reply ) {
@@ -228,8 +240,13 @@ C.open = function(allFields, openCallback0) {
228
240
negotiate ( fields . channelMax , allFields . channelMax ) ;
229
241
tunedOptions . heartbeat =
230
242
negotiate ( fields . heartbeat , allFields . heartbeat ) ;
231
- send ( defs . ConnectionTuneOk ) ;
232
- send ( defs . ConnectionOpen ) ;
243
+ try {
244
+ send ( defs . ConnectionTuneOk ) ;
245
+ send ( defs . ConnectionOpen ) ;
246
+ } catch {
247
+ // Exit, callback with error already called,
248
+ return ;
249
+ }
233
250
expect ( defs . ConnectionOpenOk , onOpenOk ) ;
234
251
break ;
235
252
default :
@@ -257,6 +274,10 @@ C.open = function(allFields, openCallback0) {
257
274
// If the server closes the connection, it's probably because of
258
275
// something we did
259
276
function endWhileOpening ( err ) {
277
+ self . stream . removeListener ( 'end' , endWhileOpening ) ;
278
+ self . stream . removeListener ( 'error' , endWhileOpening ) ;
279
+ self . stream . end ( ) ;
280
+
260
281
bail ( err || new Error ( 'Socket closed abruptly ' +
261
282
'during opening handshake' ) ) ;
262
283
}
0 commit comments