Skip to content

Commit

Permalink
net: make Server.prototype.unref() persistent
Browse files Browse the repository at this point in the history
Currently, the unref() method does not remember any state
if called before the server's handle has been created. This
commit adds state to track calls to ref() and unref().

PR-URL: #897
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
  • Loading branch information
cjihrig authored and brendanashworth committed Feb 21, 2015
1 parent 26ebe98 commit d8eb974
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ function Server(options, connectionListener) {
this._handle = null;
this._usingSlaves = false;
this._slaves = [];
this._unref = false;

this.allowHalfOpen = options.allowHalfOpen || false;
this.pauseOnConnect = !!options.pauseOnConnect;
Expand Down Expand Up @@ -1171,6 +1172,10 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
// generate connection key, this should be unique to the connection
this._connectionKey = addressType + ':' + address + ':' + port;

// unref the handle if the server was unref'ed prior to listening
if (this._unref)
this.unref();

process.nextTick(function() {
// ensure handle hasn't closed
if (self._handle)
Expand Down Expand Up @@ -1440,11 +1445,15 @@ Server.prototype._setupSlave = function(socketList) {
};

Server.prototype.ref = function() {
this._unref = false;

if (this._handle)
this._handle.ref();
};

Server.prototype.unref = function() {
this._unref = true;

if (this._handle)
this._handle.unref();
};
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-net-server-unref-persistent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var common = require('../common');
var assert = require('assert');
var net = require('net');
var closed = false;
var server = net.createServer();

// unref before listening
server.unref();
server.listen();

// If the timeout fires, that means the server held the event loop open
// and the unref() was not persistent. Close the server and fail the test.
setTimeout(function() {
closed = true;
server.close();
}, 1000).unref();

process.on('exit', function() {
assert.strictEqual(closed, false, 'server should not hold loop open');
});

0 comments on commit d8eb974

Please sign in to comment.