@@ -229,7 +229,6 @@ function Socket(options) {
229229 }
230230
231231 // shut down the socket when we're finished with it.
232- this . on ( 'finish' , onSocketFinish ) ;
233232 this . on ( '_socketEnd' , onSocketEnd ) ;
234233
235234 initSocketHandle ( this ) ;
@@ -273,39 +272,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
273272
274273function shutdownSocket ( self , callback ) {
275274 var req = new ShutdownWrap ( ) ;
276- req . oncomplete = callback ;
275+ req . oncomplete = afterShutdown ;
277276 req . handle = self . _handle ;
277+ req . callback = callback ;
278278 return self . _handle . shutdown ( req ) ;
279279}
280280
281281// the user has called .end(), and all the bytes have been
282282// sent out to the other side.
283- function onSocketFinish ( ) {
284- // If still connecting - defer handling 'finish' until 'connect' will happen
283+ Socket . prototype . _final = function ( cb ) {
284+ // If still connecting - defer handling `_final` until 'connect' will happen
285285 if ( this . connecting ) {
286- debug ( 'osF : not yet connected' ) ;
287- return this . once ( 'connect' , onSocketFinish ) ;
286+ debug ( '_final : not yet connected' ) ;
287+ return this . once ( 'connect' , ( ) => this . _final ( cb ) ) ;
288288 }
289289
290- debug ( 'onSocketFinish' ) ;
291290 if ( ! this . readable || this . _readableState . ended ) {
292- debug ( 'oSF: ended, destroy' , this . _readableState ) ;
291+ debug ( '_final: ended, destroy' , this . _readableState ) ;
292+ cb ( ) ;
293293 return this . destroy ( ) ;
294294 }
295295
296- debug ( 'oSF : not ended, call shutdown()' ) ;
296+ debug ( '_final : not ended, call shutdown()' ) ;
297297
298298 // otherwise, just shutdown, or destroy() if not possible
299- if ( ! this . _handle || ! this . _handle . shutdown )
299+ if ( ! this . _handle || ! this . _handle . shutdown ) {
300+ cb ( ) ;
300301 return this . destroy ( ) ;
302+ }
301303
302304 var err = defaultTriggerAsyncIdScope (
303- this [ async_id_symbol ] , shutdownSocket , this , afterShutdown
305+ this [ async_id_symbol ] , shutdownSocket , this , cb
304306 ) ;
305307
306308 if ( err )
307309 return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
308- }
310+ } ;
309311
310312
311313function afterShutdown ( status , handle , req ) {
@@ -314,6 +316,8 @@ function afterShutdown(status, handle, req) {
314316 debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
315317 self . _readableState ) ;
316318
319+ this . callback ( ) ;
320+
317321 // callback may come after call to destroy.
318322 if ( self . destroyed )
319323 return ;
0 commit comments