@@ -301,8 +301,9 @@ function Socket(options) {
301301
302302 // Default to *not* allowing half open sockets.
303303 options . allowHalfOpen = Boolean ( options . allowHalfOpen ) ;
304- // For backwards compat do not emit close on destroy.
305- options . emitClose = false ;
304+ // For backward compatibility, pass a flag when emitting close for
305+ // comunicating that an error occurred.
306+ options . errorFlagOnClose = true ;
306307 options . autoDestroy = true ;
307308 // Handle strings directly.
308309 options . decodeStrings = false ;
@@ -657,28 +658,20 @@ Socket.prototype._destroy = function(exception, cb) {
657658 if ( this . _handle ) {
658659 if ( this !== process . stderr )
659660 debug ( 'close handle' ) ;
660- const isException = exception ? true : false ;
661+
661662 // `bytesRead` and `kBytesWritten` should be accessible after `.destroy()`
662663 this [ kBytesRead ] = this . _handle . bytesRead ;
663664 this [ kBytesWritten ] = this . _handle . bytesWritten ;
664665
665666 this . _handle . close ( ( ) => {
666667 debug ( 'emit close' ) ;
667- if ( this . _writableState ) {
668- this . _writableState . closed = true ;
669- }
670- if ( this . _readableState ) {
671- this . _readableState . closed = true ;
672- }
673- this . emit ( 'close' , isException ) ;
668+ cb ( exception ) ;
674669 } ) ;
675670 this . _handle . onread = noop ;
676671 this . _handle = null ;
677672 this . _sockname = null ;
678- cb ( exception ) ;
679673 } else {
680674 cb ( exception ) ;
681- process . nextTick ( emitCloseNT , this ) ;
682675 }
683676
684677 if ( this . _server ) {
@@ -1657,12 +1650,6 @@ Server.prototype._emitCloseIfDrained = function() {
16571650
16581651function emitCloseNT ( self ) {
16591652 debug ( 'SERVER: emit close' ) ;
1660- if ( self . _writableState ) {
1661- self . _writableState . closed = true ;
1662- }
1663- if ( self . _readableState ) {
1664- self . _readableState . closed = true ;
1665- }
16661653 self . emit ( 'close' ) ;
16671654}
16681655
0 commit comments