@@ -15,6 +15,9 @@ const EventEmitter = require('events');
1515const  {  owner_symbol }  =  require ( 'internal/async_hooks' ) . symbols ; 
1616const  Worker  =  require ( 'internal/cluster/worker' ) ; 
1717const  {  internal,  sendHelper }  =  require ( 'internal/cluster/utils' ) ; 
18+ const  {  TIMEOUT_MAX  }  =  require ( 'internal/timers' ) ; 
19+ const  {  setInterval,  clearInterval }  =  require ( 'timers' ) ; 
20+ 
1821const  cluster  =  new  EventEmitter ( ) ; 
1922const  handles  =  new  SafeMap ( ) ; 
2023const  indexes  =  new  SafeMap ( ) ; 
@@ -160,6 +163,21 @@ function rr(message, { indexesKey, index }, cb) {
160163
161164  let  key  =  message . key ; 
162165
166+   let  fakeHandle  =  null ; 
167+ 
168+   function  ref ( )  { 
169+     if  ( ! fakeHandle )  { 
170+       fakeHandle  =  setInterval ( noop ,  TIMEOUT_MAX ) ; 
171+     } 
172+   } 
173+ 
174+   function  unref ( )  { 
175+     if  ( fakeHandle )  { 
176+       clearInterval ( fakeHandle ) ; 
177+       fakeHandle  =  null ; 
178+     } 
179+   } 
180+ 
163181  function  listen ( backlog )  { 
164182    // TODO(bnoordhuis) Send a message to the primary that tells it to 
165183    // update the backlog size. The actual backlog should probably be 
@@ -175,7 +193,11 @@ function rr(message, { indexesKey, index }, cb) {
175193    // the primary. 
176194    if  ( key  ===  undefined ) 
177195      return ; 
178- 
196+     unref ( ) ; 
197+     // If the handle is the last handle in process, 
198+     // the parent process will delete the handle when worker process exits. 
199+     // So it is ok if the close message get lost. 
200+     // See the comments of https://github.com/nodejs/node/pull/46161 
179201    send ( {  act : 'close' ,  key } ) ; 
180202    handles . delete ( key ) ; 
181203    removeIndexesKey ( indexesKey ,  index ) ; 
@@ -189,12 +211,10 @@ function rr(message, { indexesKey, index }, cb) {
189211    return  0 ; 
190212  } 
191213
192-   // Faux handle. Mimics a TCPWrap with just enough fidelity to get away 
193-   // with it. Fools net.Server into thinking that it's backed by a real 
194-   // handle. Use a noop function for ref() and unref() because the control 
195-   // channel is going to keep the worker alive anyway. 
196-   const  handle  =  {  close,  listen,  ref : noop ,  unref : noop  } ; 
197- 
214+   // Faux handle. net.Server is not associated with handle, 
215+   // so we control its state(ref or unref) by setInterval. 
216+   const  handle  =  {  close,  listen,  ref,  unref } ; 
217+   handle . ref ( ) ; 
198218  if  ( message . sockname )  { 
199219    handle . getsockname  =  getsockname ;   // TCP handles only. 
200220  } 
0 commit comments