@@ -285,20 +285,15 @@ function Socket(options) {
285
285
else
286
286
options = { ...options } ;
287
287
288
- const { allowHalfOpen } = options ;
289
-
290
- // Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
291
- options . allowHalfOpen = true ;
288
+ // Default to *not* allowing half open sockets.
289
+ options . allowHalfOpen = Boolean ( options . allowHalfOpen ) ;
292
290
// For backwards compat do not emit close on destroy.
293
291
options . emitClose = false ;
294
- options . autoDestroy = false ;
292
+ options . autoDestroy = true ;
295
293
// Handle strings directly.
296
294
options . decodeStrings = false ;
297
295
stream . Duplex . call ( this , options ) ;
298
296
299
- // Default to *not* allowing half open sockets.
300
- this . allowHalfOpen = Boolean ( allowHalfOpen ) ;
301
-
302
297
if ( options . handle ) {
303
298
this . _handle = options . handle ; // private
304
299
this [ async_id_symbol ] = getNewAsyncId ( this . _handle ) ;
@@ -416,28 +411,18 @@ Socket.prototype._final = function(cb) {
416
411
const err = this . _handle . shutdown ( req ) ;
417
412
418
413
if ( err === 1 || err === UV_ENOTCONN ) // synchronous finish
419
- return afterShutdown . call ( req , 0 ) ;
414
+ return cb ( ) ;
420
415
else if ( err !== 0 )
421
- return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
416
+ return cb ( errnoException ( err , 'shutdown' ) ) ;
422
417
} ;
423
418
424
-
425
- function afterShutdown ( status ) {
419
+ function afterShutdown ( ) {
426
420
const self = this . handle [ owner_symbol ] ;
427
421
428
422
debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
429
423
self . _readableState ) ;
430
424
431
425
this . callback ( ) ;
432
-
433
- // Callback may come after call to destroy.
434
- if ( self . destroyed )
435
- return ;
436
-
437
- if ( ! self . readable || self . readableEnded ) {
438
- debug ( 'readableState ended, destroying' ) ;
439
- self . destroy ( ) ;
440
- }
441
426
}
442
427
443
428
// Provide a better error message when we call end() as a result
@@ -452,10 +437,10 @@ function writeAfterFIN(chunk, encoding, cb) {
452
437
// eslint-disable-next-line no-restricted-syntax
453
438
const er = new Error ( 'This socket has been ended by the other party' ) ;
454
439
er . code = 'EPIPE' ;
455
- process . nextTick ( emitErrorNT , this , er ) ;
456
440
if ( typeof cb === 'function' ) {
457
441
defaultTriggerAsyncIdScope ( this [ async_id_symbol ] , process . nextTick , cb , er ) ;
458
442
}
443
+ this . destroy ( er ) ;
459
444
460
445
return false ;
461
446
}
@@ -628,12 +613,7 @@ Socket.prototype.read = function(n) {
628
613
function onReadableStreamEnd ( ) {
629
614
if ( ! this . allowHalfOpen ) {
630
615
this . write = writeAfterFIN ;
631
- if ( this . writable )
632
- this . end ( ) ;
633
- else if ( ! this . writableLength )
634
- this . destroy ( ) ;
635
- } else if ( ! this . destroyed && ! this . writable && ! this . writableLength )
636
- this . destroy ( ) ;
616
+ }
637
617
}
638
618
639
619
0 commit comments