@@ -176,7 +176,7 @@ const handleConversion = {
176176 // waiting for the NODE_HANDLE_ACK of the current passing handle.
177177 assert ( ! target . _pendingMessage ) ;
178178 target . _pendingMessage =
179- { callback, message, handle, options, retransmissions : 0 } ;
179+ { callback, message, handle, options, retransmissions : 0 } ;
180180 } else {
181181 handle . close ( ) ;
182182 }
@@ -332,8 +332,15 @@ function flushStdio(subprocess) {
332332}
333333
334334
335- function createSocket ( pipe , readable ) {
336- return net . Socket ( { handle : pipe , readable } ) ;
335+ function createSocket ( pipe , readable , watchPeerClose ) {
336+ const sock = net . Socket ( { handle : pipe , readable } ) ;
337+ if ( watchPeerClose &&
338+ process . platform !== 'win32' &&
339+ typeof pipe ?. watchPeerClose === 'function' ) {
340+ pipe . watchPeerClose ( ( ) => sock . destroy ( ) ) ;
341+ sock . once ( 'close' , ( ) => pipe . unwatchPeerClose ?. ( ) ) ;
342+ }
343+ return sock ;
337344}
338345
339346
@@ -368,7 +375,7 @@ ChildProcess.prototype.spawn = function spawn(options) {
368375
369376
370377 validateOneOf ( options . serialization , 'options.serialization' ,
371- [ undefined , 'json' , 'advanced' ] ) ;
378+ [ undefined , 'json' , 'advanced' ] ) ;
372379 const serialization = options . serialization || 'json' ;
373380
374381 if ( ipc !== undefined ) {
@@ -380,7 +387,7 @@ ChildProcess.prototype.spawn = function spawn(options) {
380387
381388 ArrayPrototypePush ( options . envPairs , `NODE_CHANNEL_FD=${ ipcFd } ` ) ;
382389 ArrayPrototypePush ( options . envPairs ,
383- `NODE_CHANNEL_SERIALIZATION_MODE=${ serialization } ` ) ;
390+ `NODE_CHANNEL_SERIALIZATION_MODE=${ serialization } ` ) ;
384391 }
385392
386393 validateString ( options . file , 'options.file' ) ;
@@ -401,10 +408,10 @@ ChildProcess.prototype.spawn = function spawn(options) {
401408
402409 // Run-time errors should emit an error, not throw an exception.
403410 if ( err === UV_EACCES ||
404- err === UV_EAGAIN ||
405- err === UV_EMFILE ||
406- err === UV_ENFILE ||
407- err === UV_ENOENT ) {
411+ err === UV_EAGAIN ||
412+ err === UV_EMFILE ||
413+ err === UV_ENFILE ||
414+ err === UV_ENOENT ) {
408415 if ( childProcessSpawn . hasSubscribers ) {
409416 childProcessSpawn . error . publish ( {
410417 process : this ,
@@ -472,9 +479,9 @@ ChildProcess.prototype.spawn = function spawn(options) {
472479
473480 if ( stream . handle ) {
474481 stream . socket = createSocket ( this . pid !== 0 ?
475- stream . handle : null , i > 0 ) ;
482+ stream . handle : null , i > 0 , i == 0 ) ;
476483
477- if ( i > 0 && this . pid !== 0 ) {
484+ if ( i > 0 && this . pid !== 0 ) {
478485 this . _closesNeeded ++ ;
479486 stream . socket . on ( 'close' , ( ) => {
480487 maybeClose ( this ) ;
@@ -494,7 +501,7 @@ ChildProcess.prototype.spawn = function spawn(options) {
494501
495502 for ( i = 0 ; i < stdio . length ; i ++ )
496503 ArrayPrototypePush ( this . stdio ,
497- stdio [ i ] . socket === undefined ? null : stdio [ i ] . socket ) ;
504+ stdio [ i ] . socket === undefined ? null : stdio [ i ] . socket ) ;
498505
499506 // Add .send() method and start listening for IPC data
500507 if ( ipc !== undefined ) setupChannel ( this , ipc , serialization ) ;
@@ -660,16 +667,16 @@ function setupChannel(target, channel, serializationMode) {
660667 target . on ( 'internalMessage' , function ( message , handle ) {
661668 // Once acknowledged - continue sending handles.
662669 if ( message . cmd === 'NODE_HANDLE_ACK' ||
663- message . cmd === 'NODE_HANDLE_NACK' ) {
670+ message . cmd === 'NODE_HANDLE_NACK' ) {
664671
665672 if ( target . _pendingMessage ) {
666673 if ( message . cmd === 'NODE_HANDLE_ACK' ) {
667674 closePendingHandle ( target ) ;
668675 } else if ( target . _pendingMessage . retransmissions ++ ===
669- MAX_HANDLE_RETRANSMISSIONS ) {
676+ MAX_HANDLE_RETRANSMISSIONS ) {
670677 closePendingHandle ( target ) ;
671678 process . emitWarning ( 'Handle did not reach the receiving process ' +
672- 'correctly' , 'SentHandleNotReceivedWarning' ) ;
679+ 'correctly' , 'SentHandleNotReceivedWarning' ) ;
673680 }
674681 }
675682
@@ -679,9 +686,9 @@ function setupChannel(target, channel, serializationMode) {
679686
680687 if ( target . _pendingMessage ) {
681688 target . _send ( target . _pendingMessage . message ,
682- target . _pendingMessage . handle ,
683- target . _pendingMessage . options ,
684- target . _pendingMessage . callback ) ;
689+ target . _pendingMessage . handle ,
690+ target . _pendingMessage . options ,
691+ target . _pendingMessage . callback ) ;
685692 }
686693
687694 for ( let i = 0 ; i < queue . length ; i ++ ) {
@@ -778,9 +785,9 @@ function setupChannel(target, channel, serializationMode) {
778785 // will result in error message that is weakly consumable.
779786 // So perform a final check on message prior to sending.
780787 if ( typeof message !== 'string' &&
781- typeof message !== 'object' &&
782- typeof message !== 'number' &&
783- typeof message !== 'boolean' ) {
788+ typeof message !== 'object' &&
789+ typeof message !== 'number' &&
790+ typeof message !== 'boolean' ) {
784791 throw new ERR_INVALID_ARG_TYPE (
785792 'message' , [ 'string' , 'object' , 'number' , 'boolean' ] , message ) ;
786793 }
@@ -841,8 +848,8 @@ function setupChannel(target, channel, serializationMode) {
841848 handle . setSimultaneousAccepts ( true ) ;
842849 }
843850 } else if ( this . _handleQueue &&
844- ! ( message && ( message . cmd === 'NODE_HANDLE_ACK' ||
845- message . cmd === 'NODE_HANDLE_NACK' ) ) ) {
851+ ! ( message && ( message . cmd === 'NODE_HANDLE_ACK' ||
852+ message . cmd === 'NODE_HANDLE_NACK' ) ) ) {
846853 // Queue request anyway to avoid out-of-order messages.
847854 ArrayPrototypePush ( this . _handleQueue , {
848855 callback : callback ,
@@ -979,11 +986,11 @@ function setupChannel(target, channel, serializationMode) {
979986const INTERNAL_PREFIX = 'NODE_' ;
980987function isInternal ( message ) {
981988 return ( message !== null &&
982- typeof message === 'object' &&
983- typeof message . cmd === 'string' &&
984- message . cmd . length > INTERNAL_PREFIX . length &&
985- StringPrototypeSlice ( message . cmd , 0 , INTERNAL_PREFIX . length ) ===
986- INTERNAL_PREFIX ) ;
989+ typeof message === 'object' &&
990+ typeof message . cmd === 'string' &&
991+ message . cmd . length > INTERNAL_PREFIX . length &&
992+ StringPrototypeSlice ( message . cmd , 0 , INTERNAL_PREFIX . length ) ===
993+ INTERNAL_PREFIX ) ;
987994}
988995
989996const nop = FunctionPrototype ;
@@ -1021,7 +1028,7 @@ function getValidStdio(stdio, sync) {
10211028 if ( stdio === 'ignore' ) {
10221029 ArrayPrototypePush ( acc , { type : 'ignore' } ) ;
10231030 } else if ( stdio === 'pipe' || stdio === 'overlapped' ||
1024- ( typeof stdio === 'number' && stdio < 0 ) ) {
1031+ ( typeof stdio === 'number' && stdio < 0 ) ) {
10251032 const a = {
10261033 type : stdio === 'overlapped' ? 'overlapped' : 'pipe' ,
10271034 readable : i === 0 ,
@@ -1061,7 +1068,7 @@ function getValidStdio(stdio, sync) {
10611068 fd : typeof stdio === 'number' ? stdio : stdio . fd ,
10621069 } ) ;
10631070 } else if ( getHandleWrapType ( stdio ) || getHandleWrapType ( stdio . handle ) ||
1064- getHandleWrapType ( stdio . _handle ) ) {
1071+ getHandleWrapType ( stdio . _handle ) ) {
10651072 const handle = getHandleWrapType ( stdio ) ?
10661073 stdio :
10671074 getHandleWrapType ( stdio . handle ) ? stdio . handle : stdio . _handle ;
0 commit comments