@@ -292,16 +292,9 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
292
292
this [ kLastUseTime ] = now ( ) ;
293
293
}
294
294
295
- /**
296
- *
297
- * @param options
298
- */
299
- _cleanup ( options : { force : boolean ; errorFactory ?: ( ) => Error ; callback ?: Callback } ) {
300
- const { callback, errorFactory, force } = options ;
295
+ _cleanup ( options : { force : boolean ; errorFactory ?: ( ) => Error } ) {
296
+ const { errorFactory, force } = options ;
301
297
if ( this . closed ) {
302
- if ( callback ) {
303
- process . nextTick ( callback ) ;
304
- }
305
298
return ;
306
299
}
307
300
@@ -315,10 +308,6 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
315
308
this [ kQueue ] . clear ( ) ;
316
309
317
310
this . emit ( Connection . CLOSE ) ;
318
-
319
- if ( callback ) {
320
- process . nextTick ( callback ) ;
321
- }
322
311
} ;
323
312
324
313
this . removeAllListeners ( Connection . PINNED ) ;
@@ -335,7 +324,10 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
335
324
}
336
325
337
326
if ( ! this [ kStream ] . writableEnded ) {
338
- this [ kStream ] . end ( completeCleanup ) ;
327
+ this [ kStream ] . end ( ( ) => {
328
+ this [ kStream ] . destroy ( ) ;
329
+ completeCleanup ( ) ;
330
+ } ) ;
339
331
} else {
340
332
completeCleanup ( ) ;
341
333
}
@@ -349,10 +341,6 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
349
341
}
350
342
351
343
onClose ( ) {
352
- if ( this . closed ) {
353
- return ;
354
- }
355
-
356
344
const message = `connection ${ this . id } to ${ this . address } closed` ;
357
345
this . _cleanup ( { force : false , errorFactory : ( ) => new MongoNetworkError ( message ) } ) ;
358
346
}
@@ -389,7 +377,10 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
389
377
390
378
// First check if the map is of invalid size
391
379
if ( this [ kQueue ] . size > 1 ) {
392
- this . onError ( new MongoRuntimeError ( INVALID_QUEUE_SIZE ) ) ;
380
+ this . _cleanup ( {
381
+ force : true ,
382
+ errorFactory : ( ) => new MongoRuntimeError ( INVALID_QUEUE_SIZE )
383
+ } ) ;
393
384
} else {
394
385
// Get the first orphaned operation description.
395
386
const entry = this [ kQueue ] . entries ( ) . next ( ) ;
@@ -469,7 +460,13 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
469
460
}
470
461
471
462
destroy ( options : DestroyOptions , callback ?: Callback ) : void {
472
- this . _cleanup ( { ...options , callback } ) ;
463
+ if ( this . closed ) {
464
+ process . nextTick ( ( ) => callback ?.( ) ) ;
465
+ return ;
466
+ }
467
+ this . once ( 'close' , ( ) => process . nextTick ( ( ) => callback ?.( ) ) ) ;
468
+ const message = `connection ${ this . id } to ${ this . address } closed` ;
469
+ this . _cleanup ( { ...options , errorFactory : ( ) => new MongoNetworkError ( message ) } ) ;
473
470
}
474
471
475
472
command (
0 commit comments