Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: make Server.prototype.unref() persistent #897

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});