@@ -236,7 +236,7 @@ function Socket(options) {
236236 }
237237
238238 // shut down the socket when we're finished with it.
239- this . on ( '_socketEnd ' , onSocketEnd ) ;
239+ this . on ( 'end ' , onReadableStreamEnd ) ;
240240
241241 initSocketHandle ( this ) ;
242242
@@ -337,32 +337,6 @@ function afterShutdown(status, handle, req) {
337337 }
338338}
339339
340- // the EOF has been received, and no more bytes are coming.
341- // if the writable side has ended already, then clean everything
342- // up.
343- function onSocketEnd ( ) {
344- // XXX Should not have to do as much in this function.
345- // ended should already be true, since this is called *after*
346- // the EOF errno and onread has eof'ed
347- debug ( 'onSocketEnd' , this . _readableState ) ;
348- this . _readableState . ended = true ;
349- if ( this . _readableState . endEmitted ) {
350- this . readable = false ;
351- maybeDestroy ( this ) ;
352- } else {
353- this . once ( 'end' , function end ( ) {
354- this . readable = false ;
355- maybeDestroy ( this ) ;
356- } ) ;
357- this . read ( 0 ) ;
358- }
359-
360- if ( ! this . allowHalfOpen ) {
361- this . write = writeAfterFIN ;
362- this . destroySoon ( ) ;
363- }
364- }
365-
366340// Provide a better error message when we call end() as a result
367341// of the other side sending a FIN. The standard 'write after end'
368342// is overly vague, and makes it seem like the user's code is to blame.
@@ -508,6 +482,12 @@ Socket.prototype.end = function(data, encoding, callback) {
508482} ;
509483
510484
485+ // Called when the 'end' event is emitted.
486+ function onReadableStreamEnd ( ) {
487+ maybeDestroy ( this ) ;
488+ }
489+
490+
511491// Call whenever we set writable=false or readable=false
512492function maybeDestroy ( socket ) {
513493 if ( ! socket . readable &&
@@ -621,10 +601,11 @@ function onread(nread, buffer) {
621601 // Do it before `maybeDestroy` for correct order of events:
622602 // `end` -> `close`
623603 self . push ( null ) ;
604+ self . read ( 0 ) ;
624605
625- if ( self . readableLength === 0 ) {
626- self . readable = false ;
627- maybeDestroy ( self ) ;
606+ if ( ! self . allowHalfOpen ) {
607+ self . write = writeAfterFIN ;
608+ self . destroySoon ( ) ;
628609 }
629610
630611 // internal end event so that we know that the actual socket
0 commit comments