Browse Source

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>
archived-io.js-v0.10
cjihrig 11 years ago
committed by Fedor Indutny
parent
commit
f1dc55d701
  1. 3
      doc/api/net.markdown
  2. 18
      lib/net.js
  3. 12
      test/simple/test-http-unix-socket.js

3
doc/api/net.markdown

@ -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()

18
lib/net.js

@ -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,

12
test/simple/test-http-unix-socket.js

@ -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();
});
}); });

Loading…
Cancel
Save