diff --git a/doc/api/dgram.markdown b/doc/api/dgram.markdown index 09bacaf098c117..ce205c0f5c6fae 100644 --- a/doc/api/dgram.markdown +++ b/doc/api/dgram.markdown @@ -82,7 +82,7 @@ are created. ### Event: 'close' -Emitted when a socket is closed with `close()`. No new `message` events will be emitted +Emitted after a socket is closed with `close()`. No new `message` events will be emitted on this socket. ### Event: 'error' @@ -228,9 +228,10 @@ shown below. }); -### socket.close() +### socket.close([callback]) -Close the underlying socket and stop listening for data on it. +Close the underlying socket and stop listening for data on it. If a callback is +provided, it is added as a listener for the ['close'](#dgram_event_close) event. ### socket.address() diff --git a/doc/api/tls.markdown b/doc/api/tls.markdown index c03845e8721d5c..d63a6eba013e96 100644 --- a/doc/api/tls.markdown +++ b/doc/api/tls.markdown @@ -602,11 +602,11 @@ when the server has been bound. See `net.Server` for more information. -### server.close() +### server.close([callback]) Stops the server from accepting new connections. This function is asynchronous, the server is finally closed when the server emits a `'close'` -event. +event. Optionally, you can pass a callback to listen for the `'close'` event. ### server.address() diff --git a/lib/dgram.js b/lib/dgram.js index ac7a054a4c4681..255b9fb5941372 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -352,12 +352,17 @@ function afterSend(err) { } -Socket.prototype.close = function() { +Socket.prototype.close = function(callback) { + if (callback) + this.on('close', callback); this._healthCheck(); this._stopReceiving(); this._handle.close(); this._handle = null; - this.emit('close'); + var self = this; + process.nextTick(function() { + self.emit('close'); + }); return this; }; diff --git a/test/parallel/test-dgram-close.js b/test/parallel/test-dgram-close.js index 1eed54c91f4106..6ac19339e4dd42 100644 --- a/test/parallel/test-dgram-close.js +++ b/test/parallel/test-dgram-close.js @@ -31,8 +31,16 @@ buf.fill(42); var socket = dgram.createSocket('udp4'); var handle = socket._handle; +var closeEvents = 0; +var closeCallbacks = 0; socket.send(buf, 0, buf.length, common.PORT, 'localhost'); -assert.strictEqual(socket.close(), socket); +assert.strictEqual(socket.close(function() { + ++closeCallbacks; +}), socket); +socket.on('close', function() { + assert.equal(closeCallbacks, 1); + ++closeEvents; +}); socket = null; // Verify that accessing handle after closure doesn't throw @@ -41,3 +49,8 @@ setImmediate(function() { console.log('Handle fd is: ', handle.fd); }); }); + +process.on('exit', function() { + assert.equal(closeEvents, 1); + assert.equal(closeCallbacks, 1); +}); diff --git a/test/parallel/test-tls-connect-simple.js b/test/parallel/test-tls-connect-simple.js index e896dd9e22a51f..55e15221b1b0c9 100644 --- a/test/parallel/test-tls-connect-simple.js +++ b/test/parallel/test-tls-connect-simple.js @@ -26,6 +26,8 @@ var fs = require('fs'); var clientConnected = 0; var serverConnected = 0; +var serverCloseCallbacks = 0; +var serverCloseEvents = 0; var options = { key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), @@ -34,7 +36,12 @@ var options = { var server = tls.Server(options, function(socket) { if (++serverConnected === 2) { - server.close(); + server.close(function() { + ++serverCloseCallbacks; + }); + server.on('close', function() { + ++serverCloseEvents; + }); } }); @@ -60,4 +67,6 @@ server.listen(common.PORT, function() { process.on('exit', function() { assert.equal(clientConnected, 2); assert.equal(serverConnected, 2); + assert.equal(serverCloseCallbacks, 1); + assert.equal(serverCloseEvents, 1); });