Skip to content

Commit 5587ff1

Browse files
committed
dns: handle implicit bind DNS errors
When send() triggers an implicit bind, the send operation is added to an internal queue. If a DNS error occurs during the bind, there is currently no mechanism for clearing the queue other than sending more data. If DNS errors keep occurring, the queue will continue to grow with no upper bound. This commit reports errors with implicit binds, and clears the queue. This should be fine, given the nature of UDP. Refs: nodejs/node-v0.x-archive#8705 Refs: #10902 PR-URL: #11036 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1f342af commit 5587ff1

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

lib/dgram.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,27 @@ function enqueue(self, toEnqueue) {
282282
// event handler that flushes the send queue after binding is done.
283283
if (!self._queue) {
284284
self._queue = [];
285-
self.once('listening', clearQueue);
285+
self.once('error', onListenError);
286+
self.once('listening', onListenSuccess);
286287
}
287288
self._queue.push(toEnqueue);
288289
return;
289290
}
290291

291292

293+
function onListenSuccess() {
294+
this.removeListener('error', onListenError);
295+
clearQueue.call(this);
296+
}
297+
298+
299+
function onListenError(err) {
300+
this.removeListener('listening', onListenSuccess);
301+
this._queue = undefined;
302+
this.emit('error', new Error('Unable to send data'));
303+
}
304+
305+
292306
function clearQueue() {
293307
const queue = this._queue;
294308
this._queue = undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
5+
const dns = require('dns');
6+
7+
// Monkey patch dns.lookup() so that it always fails.
8+
dns.lookup = function(address, family, callback) {
9+
process.nextTick(() => { callback(new Error('fake DNS')); });
10+
};
11+
12+
const socket = dgram.createSocket('udp4');
13+
let dnsFailures = 0;
14+
let sendFailures = 0;
15+
16+
process.on('exit', () => {
17+
assert.strictEqual(dnsFailures, 3);
18+
assert.strictEqual(sendFailures, 3);
19+
});
20+
21+
socket.on('error', (err) => {
22+
if (/^Error: fake DNS$/.test(err)) {
23+
// The DNS lookup should fail since it is monkey patched. At that point in
24+
// time, the send queue should be populated with the send() operation. There
25+
// should also be two listeners - this function and the dgram internal one
26+
// time error handler.
27+
dnsFailures++;
28+
assert(Array.isArray(socket._queue));
29+
assert.strictEqual(socket._queue.length, 1);
30+
assert.strictEqual(socket.listenerCount('error'), 2);
31+
return;
32+
}
33+
34+
if (/^Error: Unable to send data$/.test(err)) {
35+
// On error, the queue should be destroyed and this function should be
36+
// the only listener.
37+
sendFailures++;
38+
assert.strictEqual(socket._queue, undefined);
39+
assert.strictEqual(socket.listenerCount('error'), 1);
40+
return;
41+
}
42+
43+
common.fail(`Unexpected error: ${err}`);
44+
});
45+
46+
// Initiate a few send() operations, which will fail.
47+
socket.send('foobar', common.PORT, 'localhost');
48+
49+
process.nextTick(() => {
50+
socket.send('foobar', common.PORT, 'localhost');
51+
});
52+
53+
setImmediate(() => {
54+
socket.send('foobar', common.PORT, 'localhost');
55+
});

0 commit comments

Comments
 (0)