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