Skip to content

Commit

Permalink
dgram: call send callback asynchronously
Browse files Browse the repository at this point in the history
dgram#send callback was changed synchronously.
The PR-URL is here joyent/libuv#1358

This commit is temporary fix until libuv issue is resolved.
libuv/libuv#301

PR-URL: #1313
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
yosuke-furukawa committed May 10, 2015
1 parent aed6bce commit 18d457b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}


Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-dgram-send-callback-recursive.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 18d457b

Please sign in to comment.