@@ -249,8 +249,6 @@ function Socket(options) {
249
249
250
250
// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
251
251
options . allowHalfOpen = true ;
252
- // For backwards compat do not emit close on destroy.
253
- options . emitClose = false ;
254
252
stream . Duplex . call ( this , options ) ;
255
253
256
254
// Default to *not* allowing half open sockets.
@@ -583,33 +581,33 @@ Socket.prototype._destroy = function(exception, cb) {
583
581
clearTimeout ( s [ kTimeout ] ) ;
584
582
}
585
583
586
- debug ( 'close' ) ;
584
+ const onClosed = ( ) => {
585
+ cb ( exception ) ;
586
+
587
+ if ( this . _server ) {
588
+ COUNTER_NET_SERVER_CONNECTION_CLOSE ( this ) ;
589
+ debug ( 'has server' ) ;
590
+ this . _server . _connections -- ;
591
+ if ( this . _server . _emitCloseIfDrained ) {
592
+ this . _server . _emitCloseIfDrained ( ) ;
593
+ }
594
+ }
595
+ } ;
596
+
587
597
if ( this . _handle ) {
598
+ debug ( 'close' ) ;
588
599
if ( this !== process . stderr )
589
600
debug ( 'close handle' ) ;
590
- var isException = exception ? true : false ;
591
601
// `bytesRead` and `kBytesWritten` should be accessible after `.destroy()`
592
602
this [ kBytesRead ] = this . _handle . bytesRead ;
593
603
this [ kBytesWritten ] = this . _handle . bytesWritten ;
594
604
595
- this . _handle . close ( ( ) => {
596
- debug ( 'emit close' ) ;
597
- this . emit ( 'close' , isException ) ;
598
- } ) ;
605
+ this . _handle . close ( onClosed ) ;
599
606
this . _handle . onread = noop ;
600
607
this . _handle = null ;
601
608
this . _sockname = null ;
602
- }
603
-
604
- cb ( exception ) ;
605
-
606
- if ( this . _server ) {
607
- COUNTER_NET_SERVER_CONNECTION_CLOSE ( this ) ;
608
- debug ( 'has server' ) ;
609
- this . _server . _connections -- ;
610
- if ( this . _server . _emitCloseIfDrained ) {
611
- this . _server . _emitCloseIfDrained ( ) ;
612
- }
609
+ } else {
610
+ onClosed ( ) ;
613
611
}
614
612
} ;
615
613
0 commit comments