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: copy the array when using the list variant in send() #6804

Closed
wants to merge 4 commits 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
40 changes: 24 additions & 16 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,20 @@ function sliceBuffer(buffer, offset, length) {
}


function fixBuffer(buffer) {
for (var i = 0, l = buffer.length; i < l; i++) {
var buf = buffer[i];
function fixBufferList(list) {
const newlist = new Array(list.length);

for (var i = 0, l = list.length; i < l; i++) {
var buf = list[i];
if (typeof buf === 'string')
buffer[i] = Buffer.from(buf);
newlist[i] = Buffer.from(buf);
else if (!(buf instanceof Buffer))
return false;
return null;
else
newlist[i] = buf;
}

return true;
return newlist;
}


Expand Down Expand Up @@ -306,7 +310,8 @@ Socket.prototype.send = function(buffer,
port,
address,
callback) {
var self = this;
const self = this;
let list;

if (address || (port && typeof port !== 'function')) {
buffer = sliceBuffer(buffer, offset, length);
Expand All @@ -318,13 +323,13 @@ Socket.prototype.send = function(buffer,

if (!Array.isArray(buffer)) {
if (typeof buffer === 'string') {
buffer = [ Buffer.from(buffer) ];
list = [ Buffer.from(buffer) ];
} else if (!(buffer instanceof Buffer)) {
throw new TypeError('First argument must be a buffer or a string');
} else {
buffer = [ buffer ];
list = [ buffer ];
}
} else if (!fixBuffer(buffer)) {
} else if (!(list = fixBufferList(buffer))) {
throw new TypeError('Buffer list arguments must be buffers or strings');
}

Expand All @@ -342,20 +347,23 @@ Socket.prototype.send = function(buffer,
if (self._bindState == BIND_STATE_UNBOUND)
self.bind({port: 0, exclusive: true}, null);

if (list.length === 0)
list.push(Buffer.allocUnsafe(0));

// If the socket hasn't been bound yet, push the outbound packet onto the
// send queue and send after binding is complete.
if (self._bindState != BIND_STATE_BOUND) {
enqueue(self, [buffer, port, address, callback]);
enqueue(self, [list, port, address, callback]);
return;
}

self._handle.lookup(address, function afterDns(ex, ip) {
doSend(ex, self, ip, buffer, address, port, callback);
doSend(ex, self, ip, list, address, port, callback);
});
};


function doSend(ex, self, ip, buffer, address, port, callback) {
function doSend(ex, self, ip, list, address, port, callback) {
if (ex) {
if (typeof callback === 'function') {
callback(ex);
Expand All @@ -369,16 +377,16 @@ function doSend(ex, self, ip, buffer, address, port, callback) {
}

var req = new SendWrap();
req.buffer = buffer; // Keep reference alive.
req.list = list; // Keep reference alive.
req.address = address;
req.port = port;
if (callback) {
req.callback = callback;
req.oncomplete = afterSend;
}
var err = self._handle.send(req,
buffer,
buffer.length,
list,
list.length,
port,
ip,
!!callback);
Expand Down
2 changes: 1 addition & 1 deletion src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
args.Holder(),
args.GetReturnValue().Set(UV_EBADF));

// send(req, buffer, port, address, hasCallback)
// send(req, list, port, address, hasCallback)
CHECK(args[0]->IsObject());
CHECK(args[1]->IsArray());
CHECK(args[2]->IsUint32());
Expand Down
26 changes: 12 additions & 14 deletions test/parallel/test-dgram-send-callback-buffer-length.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
'use strict';
var common = require('../common');
var assert = require('assert');

var dgram = require('dgram');
var client, timer, buf, len, offset;
const common = require('../common');
const assert = require('assert');

const dgram = require('dgram');
const client = dgram.createSocket('udp4');

client = dgram.createSocket('udp4');

buf = Buffer.allocUnsafe(256);
offset = 20;

len = buf.length - offset;
const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));

const buf = Buffer.allocUnsafe(256);
const offset = 20;
const len = buf.length - offset;

client.send(buf, offset, len, common.PORT, '127.0.0.1', function(err, bytes) {
const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.notEqual(bytes, buf.length);
assert.equal(bytes, buf.length - offset);
clearTimeout(timer);
client.close();
});

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);
client.send(buf, offset, len, common.PORT, '127.0.0.1', messageSent);
9 changes: 4 additions & 5 deletions test/parallel/test-dgram-send-callback-multi-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));

const onMessage = common.mustCall(function(err, bytes) {
const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.equal(bytes, buf1.length + buf2.length);
clearTimeout(timer);
client.close();
});

const buf1 = Buffer.alloc(256, 'x');
const buf2 = Buffer.alloc(256, 'y');

client.on('listening', function() {
client.send([buf1, buf2], common.PORT, common.localhostIPv4, onMessage);
client.send([buf1, buf2], common.PORT, common.localhostIPv4, messageSent);
});

client.on('message', function(buf, info) {
client.on('message', common.mustCall(function onMessage(buf, info) {
const expected = Buffer.concat([buf1, buf2]);
assert.ok(buf.equals(expected), 'message was received correctly');
client.close();
});
}));

client.bind(common.PORT);
29 changes: 29 additions & 0 deletions test/parallel/test-dgram-send-empty-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

if (process.platform === 'darwin') {
common.skip('because of 17894467 Apple bug');
return;
}

const client = dgram.createSocket('udp4');

const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));
Copy link
Member

Choose a reason for hiding this comment

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

Can we get rid of the timer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It allows fast-failures, otherwise you rely on the 1 minute timeout that each tests has.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but these kind of timers have been source of flaky tests. I don't think relying in the test runner timeout is a bad idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem are not the timers, UDP messages can get lost: that's why the tests are flaky. If it has not arrived after 200ms (adjusted for platforms), it will never arrive. We might discuss on increasing those timeouts to reduce the likelyhood of missing a "right" message.
There will always be flaky tests on UDP if we want high coverage.

Copy link
Member

Choose a reason for hiding this comment

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

I understand that: it's UDP.
But I think these kind timers (the test is taking longer than expected so we throw) should only exist when testing timing related features (timers, connection timers, etc.) otherwise the test runner should catch them. I think @Trott made some good points here: nodejs/testing#27 (comment).

Anyway, I'm fine either way.

Copy link
Member Author

Choose a reason for hiding this comment

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

All tests of UDP are done in the same way. If we want to change those, we can (and probably we should). It's definite a different PR as this is becoming more and more full of stuff.

Copy link
Member

Choose a reason for hiding this comment

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

It makes sense to me.


client.on('message', common.mustCall(function onMessage(buf, info) {
const expected = Buffer.alloc(0);
assert.ok(buf.equals(expected), 'message was received correctly');
clearTimeout(timer);
client.close();
}));

client.on('listening', function() {
client.send([], common.PORT, common.localhostIPv4);
});

client.bind(common.PORT);
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-send-empty-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const client = dgram.createSocket('udp4');

client.bind(common.PORT);

client.on('message', function(buffer, bytes) {
client.on('message', common.mustCall(function onMessage(buffer, bytes) {
clearTimeout(timer);
client.close();
});
}));

const buf = Buffer.alloc(0);
client.send(buf, 0, 0, common.PORT, '127.0.0.1', function(err, len) { });
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-dgram-send-multi-buffer-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

const client = dgram.createSocket('udp4');

const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));

const onMessage = common.mustCall(function(err, bytes) {
assert.equal(bytes, buf1.length + buf2.length);
clearTimeout(timer);
});

const buf1 = Buffer.alloc(256, 'x');
const buf2 = Buffer.alloc(256, 'y');

client.on('listening', function() {
const toSend = [buf1, buf2];
client.send(toSend, common.PORT, common.localhostIPv4, onMessage);
toSend.splice(0, 2);
});

client.on('message', common.mustCall(function onMessage(buf, info) {
const expected = Buffer.concat([buf1, buf2]);
assert.ok(buf.equals(expected), 'message was received correctly');
client.close();
}));

client.bind(common.PORT);