@@ -16,6 +16,8 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
1616const Worker = require ( 'internal/cluster/worker' ) ;
1717const { internal, sendHelper } = require ( 'internal/cluster/utils' ) ;
1818const { exitCodes : { kNoFailure } } = internalBinding ( 'errors' ) ;
19+ const { TIMEOUT_MAX } = require ( 'internal/timers' ) ;
20+ const { setInterval, clearInterval } = require ( 'timers' ) ;
1921
2022const cluster = new EventEmitter ( ) ;
2123const handles = new SafeMap ( ) ;
@@ -162,6 +164,21 @@ function rr(message, { indexesKey, index }, cb) {
162164
163165 let key = message . key ;
164166
167+ let fakeHandle = null ;
168+
169+ function ref ( ) {
170+ if ( ! fakeHandle ) {
171+ fakeHandle = setInterval ( noop , TIMEOUT_MAX ) ;
172+ }
173+ }
174+
175+ function unref ( ) {
176+ if ( fakeHandle ) {
177+ clearInterval ( fakeHandle ) ;
178+ fakeHandle = null ;
179+ }
180+ }
181+
165182 function listen ( backlog ) {
166183 // TODO(bnoordhuis) Send a message to the primary that tells it to
167184 // update the backlog size. The actual backlog should probably be
@@ -177,7 +194,11 @@ function rr(message, { indexesKey, index }, cb) {
177194 // the primary.
178195 if ( key === undefined )
179196 return ;
180-
197+ unref ( ) ;
198+ // If the handle is the last handle in process,
199+ // the parent process will delete the handle when worker process exits.
200+ // So it is ok if the close message get lost.
201+ // See the comments of https://github.com/nodejs/node/pull/46161
181202 send ( { act : 'close' , key } ) ;
182203 handles . delete ( key ) ;
183204 removeIndexesKey ( indexesKey , index ) ;
@@ -191,12 +212,10 @@ function rr(message, { indexesKey, index }, cb) {
191212 return 0 ;
192213 }
193214
194- // Faux handle. Mimics a TCPWrap with just enough fidelity to get away
195- // with it. Fools net.Server into thinking that it's backed by a real
196- // handle. Use a noop function for ref() and unref() because the control
197- // channel is going to keep the worker alive anyway.
198- const handle = { close, listen, ref : noop , unref : noop } ;
199-
215+ // Faux handle. net.Server is not associated with handle,
216+ // so we control its state(ref or unref) by setInterval.
217+ const handle = { close, listen, ref, unref } ;
218+ handle . ref ( ) ;
200219 if ( message . sockname ) {
201220 handle . getsockname = getsockname ; // TCP handles only.
202221 }
0 commit comments