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

dgram and tls close() cleanup wrt. callbacks #217

Closed
wants to merge 3 commits 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
7 changes: 4 additions & 3 deletions doc/api/dgram.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions doc/api/tls.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 7 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
15 changes: 14 additions & 1 deletion test/parallel/test-dgram-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
11 changes: 10 additions & 1 deletion test/parallel/test-tls-connect-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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;
});
}
});

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