@@ -16,6 +16,7 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
16
16
const Worker = require ( 'internal/cluster/worker' ) ;
17
17
const { internal, sendHelper } = require ( 'internal/cluster/utils' ) ;
18
18
const { exitCodes : { kNoFailure } } = internalBinding ( 'errors' ) ;
19
+ const { TIMEOUT_MAX } = require ( 'internal/timers' ) ;
19
20
20
21
const cluster = new EventEmitter ( ) ;
21
22
const handles = new SafeMap ( ) ;
@@ -162,6 +163,21 @@ function rr(message, { indexesKey, index }, cb) {
162
163
163
164
let key = message . key ;
164
165
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
+
165
181
function listen ( backlog ) {
166
182
// TODO(bnoordhuis) Send a message to the primary that tells it to
167
183
// update the backlog size. The actual backlog should probably be
@@ -177,7 +193,7 @@ function rr(message, { indexesKey, index }, cb) {
177
193
// the primary.
178
194
if ( key === undefined )
179
195
return ;
180
-
196
+ unref ( ) ;
181
197
send ( { act : 'close' , key } ) ;
182
198
handles . delete ( key ) ;
183
199
removeIndexesKey ( indexesKey , index ) ;
@@ -191,12 +207,10 @@ function rr(message, { indexesKey, index }, cb) {
191
207
return 0 ;
192
208
}
193
209
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
-
210
+ // Faux handle. net.Server is not associated with handle,
211
+ // so we control its state(ref or unref) by setInterval.
212
+ const handle = { close, listen, ref, unref } ;
213
+ handle . ref ( ) ;
200
214
if ( message . sockname ) {
201
215
handle . getsockname = getsockname ; // TCP handles only.
202
216
}
0 commit comments