@@ -245,7 +245,7 @@ function Socket(options) {
245245
246246 // shut down the socket when we're finished with it.
247247 this . on ( 'finish' , onSocketFinish ) ;
248- this . on ( '_socketEnd ' , onSocketEnd ) ;
248+ this . on ( 'end ' , onReadableStreamEnd ) ;
249249
250250 initSocketHandle ( this ) ;
251251
@@ -341,32 +341,6 @@ function afterShutdown(status, handle) {
341341 }
342342}
343343
344- // the EOF has been received, and no more bytes are coming.
345- // if the writable side has ended already, then clean everything
346- // up.
347- function onSocketEnd ( ) {
348- // XXX Should not have to do as much in this function.
349- // ended should already be true, since this is called *after*
350- // the EOF errno and onread has eof'ed
351- debug ( 'onSocketEnd' , this . _readableState ) ;
352- this . _readableState . ended = true ;
353- if ( this . _readableState . endEmitted ) {
354- this . readable = false ;
355- maybeDestroy ( this ) ;
356- } else {
357- this . once ( 'end' , function end ( ) {
358- this . readable = false ;
359- maybeDestroy ( this ) ;
360- } ) ;
361- this . read ( 0 ) ;
362- }
363-
364- if ( ! this . allowHalfOpen ) {
365- this . write = writeAfterFIN ;
366- this . destroySoon ( ) ;
367- }
368- }
369-
370344// Provide a better error message when we call end() as a result
371345// of the other side sending a FIN. The standard 'write after end'
372346// is overly vague, and makes it seem like the user's code is to blame.
@@ -512,6 +486,12 @@ Socket.prototype.end = function(data, encoding, callback) {
512486} ;
513487
514488
489+ // Called when the 'end' event is emitted.
490+ function onReadableStreamEnd ( ) {
491+ maybeDestroy ( this ) ;
492+ }
493+
494+
515495// Call whenever we set writable=false or readable=false
516496function maybeDestroy ( socket ) {
517497 if ( ! socket . readable &&
@@ -625,10 +605,11 @@ function onread(nread, buffer) {
625605 // Do it before `maybeDestroy` for correct order of events:
626606 // `end` -> `close`
627607 self . push ( null ) ;
608+ self . read ( 0 ) ;
628609
629- if ( self . readableLength === 0 ) {
630- self . readable = false ;
631- maybeDestroy ( self ) ;
610+ if ( ! self . allowHalfOpen ) {
611+ self . write = writeAfterFIN ;
612+ self . destroySoon ( ) ;
632613 }
633614
634615 // internal end event so that we know that the actual socket
0 commit comments