From 18d457bd3408557a48b453f13b2b99e1ab5e7159 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Wed, 1 Apr 2015 23:32:48 +0900 Subject: [PATCH] dgram: call send callback asynchronously dgram#send callback was changed synchronously. The PR-URL is here https://github.com/joyent/libuv/pull/1358 This commit is temporary fix until libuv issue is resolved. https://github.com/libuv/libuv/issues/301 PR-URL: https://github.com/iojs/io.js/pull/1313 Reviewed-By: Ben Noordhuis Reviewed-By: Trevor Norris --- lib/dgram.js | 5 ++- .../test-dgram-send-callback-recursive.js | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-dgram-send-callback-recursive.js diff --git a/lib/dgram.js b/lib/dgram.js index e80f78da076236..1cce233eab808b 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -337,7 +337,10 @@ function afterSend(err) { if (err) { err = exceptionWithHostPort(err, 'send', this.address, this.port); } - this.callback(err, this.length); + var self = this; + setImmediate(function() { + self.callback(err, self.length); + }); } diff --git a/test/parallel/test-dgram-send-callback-recursive.js b/test/parallel/test-dgram-send-callback-recursive.js new file mode 100644 index 00000000000000..b1c9745fe88346 --- /dev/null +++ b/test/parallel/test-dgram-send-callback-recursive.js @@ -0,0 +1,38 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); + +const dgram = require('dgram'); +const client = dgram.createSocket('udp4'); +const chunk = 'abc'; +var recursiveCount = 0; +var received = 0; +const limit = 10; + +function onsend() { + if (recursiveCount > limit) { + throw new Error('infinite loop detected'); + } + if (received < limit) { + client.send( + chunk, 0, chunk.length, common.PORT, common.localhostIPv4, onsend); + } + recursiveCount++; +} + +client.on('listening', function() { + onsend(); +}); + +client.on('message', function(buf, info) { + received++; + if (received === limit) { + client.close(); + } +}); + +client.on('close', common.mustCall(function() { + assert.equal(received, limit); +})); + +client.bind(common.PORT);