@@ -29,6 +29,9 @@ module.exports = {
29
29
getSocketList
30
30
} ;
31
31
32
+ const MAX_HANDLE_RETRANSMISSIONS = 3 ;
33
+
34
+
32
35
// this object contain function to convert TCP objects to native handle objects
33
36
// and back again.
34
37
const handleConversion = {
@@ -94,17 +97,18 @@ const handleConversion = {
94
97
return handle ;
95
98
} ,
96
99
97
- postSend : function ( handle , options , target ) {
100
+ postSend : function ( message , handle , options , callback , target ) {
98
101
// Store the handle after successfully sending it, so it can be closed
99
102
// when the NODE_HANDLE_ACK is received. If the handle could not be sent,
100
103
// just close it.
101
104
if ( handle && ! options . keepOpen ) {
102
105
if ( target ) {
103
- // There can only be one _pendingHandle as passing handles are
106
+ // There can only be one _pendingMessage as passing handles are
104
107
// processed one at a time: handles are stored in _handleQueue while
105
108
// waiting for the NODE_HANDLE_ACK of the current passing handle.
106
- assert ( ! target . _pendingHandle ) ;
107
- target . _pendingHandle = handle ;
109
+ assert ( ! target . _pendingMessage ) ;
110
+ target . _pendingMessage =
111
+ { callback, message, handle, options, retransmissions : 0 } ;
108
112
} else {
109
113
handle . close ( ) ;
110
114
}
@@ -267,6 +271,11 @@ function getHandleWrapType(stream) {
267
271
return false ;
268
272
}
269
273
274
+ function closePendingHandle ( target ) {
275
+ target . _pendingMessage . handle . close ( ) ;
276
+ target . _pendingMessage = null ;
277
+ }
278
+
270
279
271
280
ChildProcess . prototype . spawn = function ( options ) {
272
281
const self = this ;
@@ -437,7 +446,7 @@ class Control extends EventEmitter {
437
446
function setupChannel ( target , channel ) {
438
447
target . _channel = channel ;
439
448
target . _handleQueue = null ;
440
- target . _pendingHandle = null ;
449
+ target . _pendingMessage = null ;
441
450
442
451
const control = new Control ( channel ) ;
443
452
@@ -489,16 +498,31 @@ function setupChannel(target, channel) {
489
498
// handlers will go through this
490
499
target . on ( 'internalMessage' , function ( message , handle ) {
491
500
// Once acknowledged - continue sending handles.
492
- if ( message . cmd === 'NODE_HANDLE_ACK' ) {
493
- if ( target . _pendingHandle ) {
494
- target . _pendingHandle . close ( ) ;
495
- target . _pendingHandle = null ;
501
+ if ( message . cmd === 'NODE_HANDLE_ACK' ||
502
+ message . cmd === 'NODE_HANDLE_NACK' ) {
503
+
504
+ if ( target . _pendingMessage ) {
505
+ if ( message . cmd === 'NODE_HANDLE_ACK' ) {
506
+ closePendingHandle ( target ) ;
507
+ } else if ( target . _pendingMessage . retransmissions ++ ===
508
+ MAX_HANDLE_RETRANSMISSIONS ) {
509
+ closePendingHandle ( target ) ;
510
+ process . emitWarning ( 'Handle did not reach the receiving process ' +
511
+ 'correctly' , 'SentHandleNotReceivedWarning' ) ;
512
+ }
496
513
}
497
514
498
515
assert ( Array . isArray ( target . _handleQueue ) ) ;
499
516
var queue = target . _handleQueue ;
500
517
target . _handleQueue = null ;
501
518
519
+ if ( target . _pendingMessage ) {
520
+ target . _send ( target . _pendingMessage . message ,
521
+ target . _pendingMessage . handle ,
522
+ target . _pendingMessage . options ,
523
+ target . _pendingMessage . callback ) ;
524
+ }
525
+
502
526
for ( var i = 0 ; i < queue . length ; i ++ ) {
503
527
var args = queue [ i ] ;
504
528
target . _send ( args . message , args . handle , args . options , args . callback ) ;
@@ -513,6 +537,12 @@ function setupChannel(target, channel) {
513
537
514
538
if ( message . cmd !== 'NODE_HANDLE' ) return ;
515
539
540
+ // It is possible that the handle is not received because of some error on
541
+ // ancillary data reception such as MSG_CTRUNC. In this case, report the
542
+ // sender about it by sending a NODE_HANDLE_NACK message.
543
+ if ( ! handle )
544
+ return target . _send ( { cmd : 'NODE_HANDLE_NACK' } , null , true ) ;
545
+
516
546
// Acknowledge handle receival. Don't emit error events (for example if
517
547
// the other side has disconnected) because this call to send() is not
518
548
// initiated by the user and it shouldn't be fatal to be unable to ACK
@@ -623,7 +653,8 @@ function setupChannel(target, channel) {
623
653
net . _setSimultaneousAccepts ( handle ) ;
624
654
}
625
655
} else if ( this . _handleQueue &&
626
- ! ( message && message . cmd === 'NODE_HANDLE_ACK' ) ) {
656
+ ! ( message && ( message . cmd === 'NODE_HANDLE_ACK' ||
657
+ message . cmd === 'NODE_HANDLE_NACK' ) ) ) {
627
658
// Queue request anyway to avoid out-of-order messages.
628
659
this . _handleQueue . push ( {
629
660
callback : callback ,
@@ -645,7 +676,7 @@ function setupChannel(target, channel) {
645
676
if ( ! this . _handleQueue )
646
677
this . _handleQueue = [ ] ;
647
678
if ( obj && obj . postSend )
648
- obj . postSend ( handle , options , target ) ;
679
+ obj . postSend ( message , handle , options , callback , target ) ;
649
680
}
650
681
651
682
req . oncomplete = function ( ) {
@@ -662,7 +693,7 @@ function setupChannel(target, channel) {
662
693
} else {
663
694
// Cleanup handle on error
664
695
if ( obj && obj . postSend )
665
- obj . postSend ( handle , options ) ;
696
+ obj . postSend ( message , handle , options , callback ) ;
666
697
667
698
if ( ! options . swallowErrors ) {
668
699
const ex = errnoException ( err , 'write' ) ;
@@ -711,10 +742,8 @@ function setupChannel(target, channel) {
711
742
// This marks the fact that the channel is actually disconnected.
712
743
this . _channel = null ;
713
744
714
- if ( this . _pendingHandle ) {
715
- this . _pendingHandle . close ( ) ;
716
- this . _pendingHandle = null ;
717
- }
745
+ if ( this . _pendingMessage )
746
+ closePendingHandle ( this ) ;
718
747
719
748
var fired = false ;
720
749
function finish ( ) {
0 commit comments