Skip to content

Commit

Permalink
cluster: add handle ref/unref stubs in rr mode
Browse files Browse the repository at this point in the history
Add ref() and unref() stub methods to the faux handle in round-robin
mode.  Fixes the following TypeError when calling `server.unref()` in
the worker:

    net.js:1521
        this._handle.unref();
                     ^
    TypeError: this._handle.unref is not a function
        at Server.unref (net.js:1521:18)

No actual reference counting is implemented.  It would effectively be
a no-op because the control channel would still keep the worker alive.

Fixes: #73
PR-URL: #2274
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
bnoordhuis committed Jul 30, 2015
1 parent 3cbb587 commit fa98b97
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,22 @@ function workerInit() {
return 0;
}

// XXX(bnoordhuis) Probably no point in implementing ref() and unref()
// because the control channel is going to keep the worker alive anyway.
function ref() {
}

function unref() {
}

// Faux handle. Mimics a TCPWrap with just enough fidelity to get away
// with it. Fools net.Server into thinking that it's backed by a real
// handle.
var handle = {
close: close,
listen: listen
listen: listen,
ref: ref,
unref: unref,
};
if (message.sockname) {
handle.getsockname = getsockname; // TCP handles only.
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-cluster-rr-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');

if (cluster.isMaster) {
cluster.fork().on('message', function(msg) {
if (msg === 'done') this.kill();
});
} else {
const server = net.createServer(assert.fail);
server.listen(common.PORT, function() {
server.unref();
server.ref();
server.close(function() {
process.send('done');
});
});
}

0 comments on commit fa98b97

Please sign in to comment.