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

dns: handle implicit bind DNS errors #11036

Merged
merged 1 commit into from
Jan 30, 2017
Merged
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
16 changes: 15 additions & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,27 @@ function enqueue(self, toEnqueue) {
// event handler that flushes the send queue after binding is done.
if (!self._queue) {
self._queue = [];
self.once('listening', clearQueue);
self.once('error', onListenError);
self.once('listening', onListenSuccess);
}
self._queue.push(toEnqueue);
return;
}


function onListenSuccess() {
this.removeListener('error', onListenError);
clearQueue.call(this);
}


function onListenError(err) {
this.removeListener('listening', onListenSuccess);
this._queue = undefined;
this.emit('error', new Error('Unable to send data'));
}


function clearQueue() {
const queue = this._queue;
this._queue = undefined;
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-dgram-implicit-bind-failure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const dns = require('dns');

// Monkey patch dns.lookup() so that it always fails.
dns.lookup = function(address, family, callback) {
process.nextTick(() => { callback(new Error('fake DNS')); });
};

const socket = dgram.createSocket('udp4');
let dnsFailures = 0;
let sendFailures = 0;

process.on('exit', () => {
assert.strictEqual(dnsFailures, 3);
assert.strictEqual(sendFailures, 3);
});

socket.on('error', (err) => {
Copy link
Member

Choose a reason for hiding this comment

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

common.mustCall()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need in this case, because two types of errors are counted and checked on process exit.

if (/^Error: fake DNS$/.test(err)) {
// The DNS lookup should fail since it is monkey patched. At that point in
// time, the send queue should be populated with the send() operation. There
// should also be two listeners - this function and the dgram internal one
// time error handler.
dnsFailures++;
assert(Array.isArray(socket._queue));
assert.strictEqual(socket._queue.length, 1);
assert.strictEqual(socket.listenerCount('error'), 2);
return;
}

if (/^Error: Unable to send data$/.test(err)) {
// On error, the queue should be destroyed and this function should be
// the only listener.
sendFailures++;
assert.strictEqual(socket._queue, undefined);
assert.strictEqual(socket.listenerCount('error'), 1);
return;
}

common.fail(`Unexpected error: ${err}`);
});

// Initiate a few send() operations, which will fail.
socket.send('foobar', common.PORT, 'localhost');

process.nextTick(() => {
socket.send('foobar', common.PORT, 'localhost');
});

setImmediate(() => {
socket.send('foobar', common.PORT, 'localhost');
});