@@ -24,6 +24,7 @@ var EventEmitter = require('events').EventEmitter;
24
24
var net = require ( 'net' ) ;
25
25
var dgram = require ( 'dgram' ) ;
26
26
var Process = process . binding ( 'process_wrap' ) . Process ;
27
+ var assert = require ( 'assert' ) ;
27
28
var util = require ( 'util' ) ;
28
29
var constants ; // if (!constants) constants = process.binding('constants');
29
30
@@ -321,6 +322,7 @@ function handleMessage(target, message, handle) {
321
322
322
323
function setupChannel ( target , channel ) {
323
324
target . _channel = channel ;
325
+ target . _handleQueue = null ;
324
326
325
327
var decoder = new StringDecoder ( 'utf8' ) ;
326
328
var jsonBuffer = '' ;
@@ -358,8 +360,22 @@ function setupChannel(target, channel) {
358
360
359
361
// handlers will go through this
360
362
target . on ( 'internalMessage' , function ( message , handle ) {
363
+ // Once acknowledged - continue sending handles.
364
+ if ( message . cmd === 'NODE_HANDLE_ACK' ) {
365
+ assert ( Array . isArray ( target . _handleQueue ) ) ;
366
+ var queue = target . _handleQueue ;
367
+ target . _handleQueue = null ;
368
+ queue . forEach ( function ( args ) {
369
+ target . send ( args . message , args . handle ) ;
370
+ } ) ;
371
+ return ;
372
+ }
373
+
361
374
if ( message . cmd !== 'NODE_HANDLE' ) return ;
362
375
376
+ // Acknowledge handle receival.
377
+ target . send ( { cmd : 'NODE_HANDLE_ACK' } ) ;
378
+
363
379
var obj = handleConversion [ message . type ] ;
364
380
365
381
// Update simultaneous accepts on Windows
@@ -389,6 +405,7 @@ function setupChannel(target, channel) {
389
405
// this message will be handled by an internalMessage event handler
390
406
message = {
391
407
cmd : 'NODE_HANDLE' ,
408
+ type : null ,
392
409
msg : message
393
410
} ;
394
411
@@ -407,6 +424,12 @@ function setupChannel(target, channel) {
407
424
throw new TypeError ( "This handle type can't be sent" ) ;
408
425
}
409
426
427
+ // Queue-up message and handle if we haven't received ACK yet.
428
+ if ( this . _handleQueue ) {
429
+ this . _handleQueue . push ( { message : message . msg , handle : handle } ) ;
430
+ return ;
431
+ }
432
+
410
433
var obj = handleConversion [ message . type ] ;
411
434
412
435
// convert TCP object to native handle object
@@ -416,6 +439,10 @@ function setupChannel(target, channel) {
416
439
if ( obj . simultaneousAccepts ) {
417
440
net . _setSimultaneousAccepts ( handle ) ;
418
441
}
442
+ } else if ( this . _handleQueue ) {
443
+ // Queue request anyway to avoid out-of-order messages.
444
+ this . _handleQueue . push ( { message : message , handle : null } ) ;
445
+ return ;
419
446
}
420
447
421
448
var string = JSON . stringify ( message ) + '\n' ;
@@ -426,6 +453,8 @@ function setupChannel(target, channel) {
426
453
'write' ,
427
454
'cannot write to IPC channel.' ) ;
428
455
this . emit ( 'error' , er ) ;
456
+ } else if ( handle && ! this . _handleQueue ) {
457
+ this . _handleQueue = [ ] ;
429
458
}
430
459
431
460
if ( obj && obj . postSend ) {
0 commit comments