net: don't throw on net.Server.close()

When close() is called on a non-listening server, a synchronous
error is thrown. This commit causes the error to be passed to
the asynchronous callback function instead.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
cjihrig 2014-05-15 22:48:27 -04:00 committed by Fedor Indutny
parent 655ec2113a
commit f1dc55d701
3 changed files with 19 additions and 14 deletions

View File

@ -206,7 +206,8 @@ Stops the server from accepting new connections and keeps existing
connections. This function is asynchronous, the server is finally connections. This function is asynchronous, the server is finally
closed when all connections are ended and the server emits a `'close'` closed when all connections are ended and the server emits a `'close'`
event. Optionally, you can pass a callback to listen for the `'close'` event. Optionally, you can pass a callback to listen for the `'close'`
event. event. If present, the callback is invoked with any potential error
as the first and only argument.
### server.address() ### server.address()

View File

@ -1341,16 +1341,20 @@ Server.prototype.close = function(cb) {
self._emitCloseIfDrained(); self._emitCloseIfDrained();
} }
if (!this._handle) { if (cb) {
// Throw error. Follows net_legacy behaviour. if (!this._handle) {
throw new Error('Not running'); this.once('close', function() {
cb(new Error('Not running'));
});
} else {
this.once('close', cb);
}
} }
if (cb) { if (this._handle) {
this.once('close', cb); this._handle.close();
this._handle = null;
} }
this._handle.close();
this._handle = null;
if (this._usingSlaves) { if (this._usingSlaves) {
var self = this, var self = this,

View File

@ -62,7 +62,12 @@ server.listen(common.PIPE, function() {
res.on('end', function() { res.on('end', function() {
assert.equal(res.body, 'hello world\n'); assert.equal(res.body, 'hello world\n');
body_ok = true; body_ok = true;
server.close(); server.close(function(error) {
assert.equal(error, undefined);
server.close(function(error) {
assert.equal(error && error.message, 'Not running');
});
});
}); });
}); });
@ -79,9 +84,4 @@ process.on('exit', function() {
assert.ok(status_ok); assert.ok(status_ok);
assert.ok(headers_ok); assert.ok(headers_ok);
assert.ok(body_ok); assert.ok(body_ok);
// Double close should throw. Follows net_legacy behaviour.
assert.throws(function() {
server.close();
});
}); });