@@ -473,14 +473,21 @@ function masterInit() {
473473 // Stop processing if worker already disconnecting
474474 if ( worker . exitedAfterDisconnect )
475475 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 ( ':' ) ;
482483 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 ) {
484491 var constructor = RoundRobinHandle ;
485492 // UDP is exempt from round-robin connection balancing for what should
486493 // be obvious reasons: it's connectionless. There is nothing to send to
@@ -501,15 +508,45 @@ function masterInit() {
501508 if ( ! handle . data ) handle . data = message . data ;
502509
503510 // 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+
505538 reply = util . _extend ( {
506539 errno : errno ,
507540 key : key ,
508541 ack : message . seq ,
509- data : handles [ key ] . data
542+ data : data
510543 } , 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_ ) ;
513550 } ) ;
514551 }
515552
@@ -571,18 +608,23 @@ function workerInit() {
571608
572609 // obj is a net#Server or a dgram#Socket object.
573610 cluster . _getServer = function ( obj , options , cb ) {
574- const indexesKey = [ options . address ,
611+ var index ;
612+ if ( + options . port !== 0 ) {
613+ var indexesKey = [ options . address ,
575614 options . port ,
576615 options . addressType ,
577616 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+ }
582624
583625 const message = util . _extend ( {
584626 act : 'queryServer' ,
585- index : indexes [ indexesKey ] ,
627+ index : index ,
586628 data : null
587629 } , options ) ;
588630
@@ -591,6 +633,16 @@ function workerInit() {
591633 send ( message , function ( reply , handle ) {
592634 if ( obj . _setServerData ) obj . _setServerData ( reply . data ) ;
593635
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+ }
594646 if ( handle )
595647 shared ( reply , handle , indexesKey , cb ) ; // Shared listen socket.
596648 else
0 commit comments