@@ -473,14 +473,21 @@ function masterInit() {
473
473
// Stop processing if worker already disconnecting
474
474
if ( worker . exitedAfterDisconnect )
475
475
return ;
476
- var args = [ message . address ,
477
- message . port ,
478
- message . addressType ,
479
- message . fd ,
480
- message . index ] ;
481
- var key = args . join ( ':' ) ;
476
+
477
+ const port = + message . port ;
478
+ var key = [ message . address ,
479
+ message . port ,
480
+ message . addressType ,
481
+ message . fd ,
482
+ message . index ] . join ( ':' ) ;
482
483
var handle = handles [ key ] ;
483
- if ( handle === undefined ) {
484
+
485
+ // Keys containing port zero are special cases in that they represent
486
+ // temporary placeholders in the list of bound handles until the OS actually
487
+ // assigns the real port number. In these cases we must always create a new
488
+ // handle since there is no way the handle could be shared until the real
489
+ // port number is received.
490
+ if ( handle === undefined || port === 0 ) {
484
491
var constructor = RoundRobinHandle ;
485
492
// UDP is exempt from round-robin connection balancing for what should
486
493
// be obvious reasons: it's connectionless. There is nothing to send to
@@ -501,15 +508,45 @@ function masterInit() {
501
508
if ( ! handle . data ) handle . data = message . data ;
502
509
503
510
// Set custom server data
504
- handle . add ( worker , function ( errno , reply , handle ) {
511
+ handle . add ( worker , function ( errno , reply , handle_ ) {
512
+ var data ;
513
+ if ( port === 0 ) {
514
+ delete handles [ key ] ;
515
+ var port_ ;
516
+ if ( reply && reply . sockname && reply . sockname . port ) {
517
+ port_ = reply . sockname . port ;
518
+ } else if ( handle_ && handle_ . getsockname ) {
519
+ const out = { } ;
520
+ handle_ . getsockname ( out ) ;
521
+ port_ = out . port ;
522
+ } else {
523
+ port_ = message . port ;
524
+ }
525
+ key = [ message . address ,
526
+ port_ ,
527
+ message . addressType ,
528
+ message . fd ,
529
+ message . index ] . join ( ':' ) ;
530
+ if ( ! errno )
531
+ handles [ key ] = handle ;
532
+ data = handle . data ;
533
+ handle . key = key ;
534
+ } else {
535
+ data = handles [ key ] . data ;
536
+ }
537
+
505
538
reply = util . _extend ( {
506
539
errno : errno ,
507
540
key : key ,
508
541
ack : message . seq ,
509
- data : handles [ key ] . data
542
+ data : data
510
543
} , reply ) ;
511
- if ( errno ) delete handles [ key ] ; // Gives other workers a chance to retry.
512
- send ( worker , reply , handle ) ;
544
+
545
+ // Gives other workers a chance to retry.
546
+ if ( errno && port !== 0 )
547
+ delete handles [ key ] ;
548
+
549
+ send ( worker , reply , handle_ ) ;
513
550
} ) ;
514
551
}
515
552
@@ -571,18 +608,23 @@ function workerInit() {
571
608
572
609
// obj is a net#Server or a dgram#Socket object.
573
610
cluster . _getServer = function ( obj , options , cb ) {
574
- const indexesKey = [ options . address ,
611
+ var index ;
612
+ if ( + options . port !== 0 ) {
613
+ var indexesKey = [ options . address ,
575
614
options . port ,
576
615
options . addressType ,
577
616
options . fd ] . join ( ':' ) ;
578
- if ( indexes [ indexesKey ] === undefined )
579
- indexes [ indexesKey ] = 0 ;
580
- else
581
- indexes [ indexesKey ] ++ ;
617
+ if ( indexes [ indexesKey ] === undefined )
618
+ index = indexes [ indexesKey ] = 0 ;
619
+ else
620
+ index = ++ indexes [ indexesKey ] ;
621
+ } else {
622
+ index = 0 ;
623
+ }
582
624
583
625
const message = util . _extend ( {
584
626
act : 'queryServer' ,
585
- index : indexes [ indexesKey ] ,
627
+ index : index ,
586
628
data : null
587
629
} , options ) ;
588
630
@@ -591,6 +633,16 @@ function workerInit() {
591
633
send ( message , function ( reply , handle ) {
592
634
if ( obj . _setServerData ) obj . _setServerData ( reply . data ) ;
593
635
636
+ if ( + options . port === 0 && reply . sockname ) {
637
+ indexesKey = [ options . address ,
638
+ reply . sockname . port ,
639
+ options . addressType ,
640
+ options . fd ] . join ( ':' ) ;
641
+ if ( indexes [ indexesKey ] === undefined )
642
+ index = indexes [ indexesKey ] = 0 ;
643
+ else
644
+ index = ++ indexes [ indexesKey ] ;
645
+ }
594
646
if ( handle )
595
647
shared ( reply , handle , indexesKey , cb ) ; // Shared listen socket.
596
648
else
0 commit comments