@@ -229,7 +229,6 @@ function Socket(options) {
229
229
}
230
230
231
231
// shut down the socket when we're finished with it.
232
- this . on ( 'finish' , onSocketFinish ) ;
233
232
this . on ( '_socketEnd' , onSocketEnd ) ;
234
233
235
234
initSocketHandle ( this ) ;
@@ -273,39 +272,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
273
272
274
273
function shutdownSocket ( self , callback ) {
275
274
var req = new ShutdownWrap ( ) ;
276
- req . oncomplete = callback ;
275
+ req . oncomplete = afterShutdown ;
277
276
req . handle = self . _handle ;
277
+ req . callback = callback ;
278
278
return self . _handle . shutdown ( req ) ;
279
279
}
280
280
281
281
// the user has called .end(), and all the bytes have been
282
282
// 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
285
285
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 ) ) ;
288
288
}
289
289
290
- debug ( 'onSocketFinish' ) ;
291
290
if ( ! this . readable || this . _readableState . ended ) {
292
- debug ( 'oSF: ended, destroy' , this . _readableState ) ;
291
+ debug ( '_final: ended, destroy' , this . _readableState ) ;
292
+ cb ( ) ;
293
293
return this . destroy ( ) ;
294
294
}
295
295
296
- debug ( 'oSF : not ended, call shutdown()' ) ;
296
+ debug ( '_final : not ended, call shutdown()' ) ;
297
297
298
298
// otherwise, just shutdown, or destroy() if not possible
299
- if ( ! this . _handle || ! this . _handle . shutdown )
299
+ if ( ! this . _handle || ! this . _handle . shutdown ) {
300
+ cb ( ) ;
300
301
return this . destroy ( ) ;
302
+ }
301
303
302
304
var err = defaultTriggerAsyncIdScope (
303
- this [ async_id_symbol ] , shutdownSocket , this , afterShutdown
305
+ this [ async_id_symbol ] , shutdownSocket , this , cb
304
306
) ;
305
307
306
308
if ( err )
307
309
return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
308
- }
310
+ } ;
309
311
310
312
311
313
function afterShutdown ( status , handle , req ) {
@@ -314,6 +316,8 @@ function afterShutdown(status, handle, req) {
314
316
debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
315
317
self . _readableState ) ;
316
318
319
+ this . callback ( ) ;
320
+
317
321
// callback may come after call to destroy.
318
322
if ( self . destroyed )
319
323
return ;
0 commit comments