Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: call send callback asynchronously #1313

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,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);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Would there still be the same problem if you used process.nextTick()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried earlier today. setImmediate() works but process.nextTick() still hangs.

From some ad hoc testing I got the impression that the event loop is making some progress - i.e. it's not that dgram.send() is truly synchronous - but it's not making enough progress for the setTimeout timer in benchmark/net/dgram.js to fire.

}


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);