@@ -256,7 +256,6 @@ function Socket(options) {
256256 }
257257
258258 // shut down the socket when we're finished with it.
259- this . on ( 'finish' , onSocketFinish ) ;
260259 this . on ( '_socketEnd' , onSocketEnd ) ;
261260
262261 initSocketHandle ( this ) ;
@@ -303,39 +302,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
303302
304303function shutdownSocket ( self , callback ) {
305304 var req = new ShutdownWrap ( ) ;
306- req . oncomplete = callback ;
305+ req . oncomplete = afterShutdown ;
307306 req . handle = self . _handle ;
307+ req . callback = callback ;
308308 return self . _handle . shutdown ( req ) ;
309309}
310310
311311// the user has called .end(), and all the bytes have been
312312// sent out to the other side.
313- function onSocketFinish ( ) {
314- // If still connecting - defer handling 'finish' until 'connect' will happen
313+ Socket . prototype . _final = function ( cb ) {
314+ // If still connecting - defer handling `_final` until 'connect' will happen
315315 if ( this . connecting ) {
316- debug ( 'osF : not yet connected' ) ;
317- return this . once ( 'connect' , onSocketFinish ) ;
316+ debug ( '_final : not yet connected' ) ;
317+ return this . once ( 'connect' , ( ) => this . _final ( cb ) ) ;
318318 }
319319
320- debug ( 'onSocketFinish' ) ;
321320 if ( ! this . readable || this . _readableState . ended ) {
322- debug ( 'oSF: ended, destroy' , this . _readableState ) ;
321+ debug ( '_final: ended, destroy' , this . _readableState ) ;
322+ cb ( ) ;
323323 return this . destroy ( ) ;
324324 }
325325
326- debug ( 'oSF : not ended, call shutdown()' ) ;
326+ debug ( '_final : not ended, call shutdown()' ) ;
327327
328328 // otherwise, just shutdown, or destroy() if not possible
329- if ( ! this . _handle || ! this . _handle . shutdown )
329+ if ( ! this . _handle || ! this . _handle . shutdown ) {
330+ cb ( ) ;
330331 return this . destroy ( ) ;
332+ }
331333
332334 var err = defaultTriggerAsyncIdScope (
333- this [ async_id_symbol ] , shutdownSocket , this , afterShutdown
335+ this [ async_id_symbol ] , shutdownSocket , this , cb
334336 ) ;
335337
336338 if ( err )
337339 return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
338- }
340+ } ;
339341
340342
341343function afterShutdown ( status , handle ) {
@@ -344,6 +346,8 @@ function afterShutdown(status, handle) {
344346 debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
345347 self . _readableState ) ;
346348
349+ this . callback ( ) ;
350+
347351 // callback may come after call to destroy.
348352 if ( self . destroyed )
349353 return ;
0 commit comments