From 7349d7fd998cba97f3b6c76376862ae81b5d6418 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 29 Dec 2014 21:20:54 -0800 Subject: [PATCH] dgram: make 'close' event async Emit the close event asynchronously, after the close, as it is with the net/http close events. PR-URL: https://github.com/iojs/io.js/pull/217 Reviewed-By: Ben Noordhuis --- lib/dgram.js | 5 ++++- test/parallel/test-dgram-close.js | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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); +});