Skip to content

Commit 0d95e73

Browse files
committed
dgram: do not emit error for failed DNS lookups in send()
Modifies the dgram send() method to not emit errors when a DNS lookup fails if there is no callback. Given that the same UDP socket can be used to send messages to different hosts, the socket can be reused even if one of those send() fails. This slightly changes the behavior of a stable API, so that it behaves as users would expect to. This is based of @chrisdickinson nodejs/node-v0.x-archive#7738. Discussion in nodejs/node-v0.x-archive#4846.
1 parent 1ff1987 commit 0d95e73

File tree

3 files changed

+36
-59
lines changed

3 files changed

+36
-59
lines changed

lib/dgram.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,8 @@ Socket.prototype.send = function(buffer,
299299

300300
self._handle.lookup(address, function(ex, ip) {
301301
if (ex) {
302-
if (callback) {
302+
if (typeof callback === 'function') {
303303
callback(ex);
304-
305-
if (self.listeners('error').length)
306-
self.emit('error', ex);
307-
308304
return;
309305
}
310306

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
var common = require('../common');
3+
var assert = require('assert');
4+
var dgram = require('dgram');
5+
var dns = require('dns');
6+
7+
var socket = dgram.createSocket('udp4');
8+
var buffer = new Buffer('gary busey');
9+
10+
dns.setServers([]);
11+
12+
socket.once('error', onEvent);
13+
14+
// assert that:
15+
// * callbacks act as "error" listeners if given.
16+
// * error is never emitter for missing dns entries
17+
// if a callback that handles error is present
18+
// * error is emitted if a callback with no argument is passed
19+
socket.send(buffer, 0, buffer.length, 100, 'dne.example.com', callbackOnly);
20+
21+
function callbackOnly(err) {
22+
assert.ok(err);
23+
socket.removeListener('error', onEvent);
24+
socket.on('error', onError);
25+
socket.send(buffer, 0, buffer.length, 100, 'dne.example.com');
26+
}
27+
28+
function onEvent(err) {
29+
assert.fail('Error should not be emitted if there is callback');
30+
}
31+
32+
function onError(err) {
33+
assert.ok(err);
34+
socket.close();
35+
}

test/simple/test-dgram-send-cb-quelches-error.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)