@@ -236,7 +236,6 @@ function Socket(options) {
236236 }
237237
238238 // shut down the socket when we're finished with it.
239- this . on ( 'finish' , onSocketFinish ) ;
240239 this . on ( '_socketEnd' , onSocketEnd ) ;
241240
242241 initSocketHandle ( this ) ;
@@ -280,39 +279,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
280279
281280function shutdownSocket ( self , callback ) {
282281 var req = new ShutdownWrap ( ) ;
283- req . oncomplete = callback ;
282+ req . oncomplete = afterShutdown ;
284283 req . handle = self . _handle ;
284+ req . callback = callback ;
285285 return self . _handle . shutdown ( req ) ;
286286}
287287
288288// the user has called .end(), and all the bytes have been
289289// sent out to the other side.
290- function onSocketFinish ( ) {
291- // If still connecting - defer handling 'finish' until 'connect' will happen
290+ Socket . prototype . _final = function ( cb ) {
291+ // If still connecting - defer handling `_final` until 'connect' will happen
292292 if ( this . connecting ) {
293- debug ( 'osF : not yet connected' ) ;
294- return this . once ( 'connect' , onSocketFinish ) ;
293+ debug ( '_final : not yet connected' ) ;
294+ return this . once ( 'connect' , ( ) => this . _final ( cb ) ) ;
295295 }
296296
297- debug ( 'onSocketFinish' ) ;
298297 if ( ! this . readable || this . _readableState . ended ) {
299- debug ( 'oSF: ended, destroy' , this . _readableState ) ;
298+ debug ( '_final: ended, destroy' , this . _readableState ) ;
299+ cb ( ) ;
300300 return this . destroy ( ) ;
301301 }
302302
303- debug ( 'oSF : not ended, call shutdown()' ) ;
303+ debug ( '_final : not ended, call shutdown()' ) ;
304304
305305 // otherwise, just shutdown, or destroy() if not possible
306- if ( ! this . _handle || ! this . _handle . shutdown )
306+ if ( ! this . _handle || ! this . _handle . shutdown ) {
307+ cb ( ) ;
307308 return this . destroy ( ) ;
309+ }
308310
309311 var err = defaultTriggerAsyncIdScope (
310- this [ async_id_symbol ] , shutdownSocket , this , afterShutdown
312+ this [ async_id_symbol ] , shutdownSocket , this , cb
311313 ) ;
312314
313315 if ( err )
314316 return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
315- }
317+ } ;
316318
317319
318320function afterShutdown ( status , handle , req ) {
@@ -321,6 +323,8 @@ function afterShutdown(status, handle, req) {
321323 debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
322324 self . _readableState ) ;
323325
326+ this . callback ( ) ;
327+
324328 // callback may come after call to destroy.
325329 if ( self . destroyed )
326330 return ;
0 commit comments