Skip to content

Commit 18d457b

Browse files
dgram: call send callback asynchronously
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: nodejs#1313 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent aed6bce commit 18d457b

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/dgram.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ function afterSend(err) {
337337
if (err) {
338338
err = exceptionWithHostPort(err, 'send', this.address, this.port);
339339
}
340-
this.callback(err, this.length);
340+
var self = this;
341+
setImmediate(function() {
342+
self.callback(err, self.length);
343+
});
341344
}
342345

343346

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const dgram = require('dgram');
6+
const client = dgram.createSocket('udp4');
7+
const chunk = 'abc';
8+
var recursiveCount = 0;
9+
var received = 0;
10+
const limit = 10;
11+
12+
function onsend() {
13+
if (recursiveCount > limit) {
14+
throw new Error('infinite loop detected');
15+
}
16+
if (received < limit) {
17+
client.send(
18+
chunk, 0, chunk.length, common.PORT, common.localhostIPv4, onsend);
19+
}
20+
recursiveCount++;
21+
}
22+
23+
client.on('listening', function() {
24+
onsend();
25+
});
26+
27+
client.on('message', function(buf, info) {
28+
received++;
29+
if (received === limit) {
30+
client.close();
31+
}
32+
});
33+
34+
client.on('close', common.mustCall(function() {
35+
assert.equal(received, limit);
36+
}));
37+
38+
client.bind(common.PORT);

0 commit comments

Comments
 (0)