@@ -251,7 +251,6 @@ function Socket(options) {
251251  } 
252252
253253  // shut down the socket when we're finished with it. 
254-   this . on ( 'finish' ,  onSocketFinish ) ; 
255254  this . on ( 'end' ,  onReadableStreamEnd ) ; 
256255
257256  initSocketHandle ( this ) ; 
@@ -292,39 +291,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
292291
293292function  shutdownSocket ( self ,  callback )  { 
294293  var  req  =  new  ShutdownWrap ( ) ; 
295-   req . oncomplete  =  callback ; 
294+   req . oncomplete  =  afterShutdown ; 
296295  req . handle  =  self . _handle ; 
296+   req . callback  =  callback ; 
297297  return  self . _handle . shutdown ( req ) ; 
298298} 
299299
300300// the user has called .end(), and all the bytes have been 
301301// sent out to the other side. 
302- function   onSocketFinish ( )  { 
303-   // If still connecting - defer handling 'finish'  until 'connect' will happen 
302+ Socket . prototype . _final   =   function ( cb )  { 
303+   // If still connecting - defer handling `_final`  until 'connect' will happen 
304304  if  ( this . connecting )  { 
305-     debug ( 'osF : not yet connected' ) ; 
306-     return  this . once ( 'connect' ,  onSocketFinish ) ; 
305+     debug ( '_final : not yet connected' ) ; 
306+     return  this . once ( 'connect' ,  ( )   =>   this . _final ( cb ) ) ; 
307307  } 
308308
309-   debug ( 'onSocketFinish' ) ; 
310309  if  ( ! this . readable  ||  this . _readableState . ended )  { 
311-     debug ( 'oSF: ended, destroy' ,  this . _readableState ) ; 
310+     debug ( '_final: ended, destroy' ,  this . _readableState ) ; 
311+     cb ( ) ; 
312312    return  this . destroy ( ) ; 
313313  } 
314314
315-   debug ( 'oSF : not ended, call shutdown()' ) ; 
315+   debug ( '_final : not ended, call shutdown()' ) ; 
316316
317317  // otherwise, just shutdown, or destroy() if not possible 
318-   if  ( ! this . _handle  ||  ! this . _handle . shutdown ) 
318+   if  ( ! this . _handle  ||  ! this . _handle . shutdown )  { 
319+     cb ( ) ; 
319320    return  this . destroy ( ) ; 
321+   } 
320322
321323  var  err  =  defaultTriggerAsyncIdScope ( 
322-     this [ async_id_symbol ] ,  shutdownSocket ,  this ,  afterShutdown 
324+     this [ async_id_symbol ] ,  shutdownSocket ,  this ,  cb 
323325  ) ; 
324326
325327  if  ( err ) 
326328    return  this . destroy ( errnoException ( err ,  'shutdown' ) ) ; 
327- } 
329+ } ; 
328330
329331
330332function  afterShutdown ( status ,  handle )  { 
@@ -333,6 +335,8 @@ function afterShutdown(status, handle) {
333335  debug ( 'afterShutdown destroyed=%j' ,  self . destroyed , 
334336        self . _readableState ) ; 
335337
338+   this . callback ( ) ; 
339+ 
336340  // callback may come after call to destroy. 
337341  if  ( self . destroyed ) 
338342    return ; 
0 commit comments