diff --git a/lib/dgram.js b/lib/dgram.js index ac7a054a4c..75e494cbf2 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -357,7 +357,10 @@ Socket.prototype.close = function() { 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 1eed54c91f..14204bf05e 100644 --- a/test/parallel/test-dgram-close.js +++ b/test/parallel/test-dgram-close.js @@ -31,8 +31,12 @@ buf.fill(42); var socket = dgram.createSocket('udp4'); var handle = socket._handle; +var closeEvents = 0; socket.send(buf, 0, buf.length, common.PORT, 'localhost'); assert.strictEqual(socket.close(), socket); +socket.on('close', function() { + ++closeEvents; +}); socket = null; // Verify that accessing handle after closure doesn't throw @@ -41,3 +45,7 @@ setImmediate(function() { console.log('Handle fd is: ', handle.fd); }); }); + +process.on('exit', function() { + assert.equal(closeEvents, 1); +});