Skip to content

Commit 5d6c74e

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 5d6c74e

File tree

3 files changed

+35
-59
lines changed

3 files changed

+35
-59
lines changed

lib/dgram.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ Socket.prototype.sendto = function(buffer,
233233
this.send(buffer, offset, length, port, address, callback);
234234
};
235235

236-
237236
Socket.prototype.send = function(buffer,
238237
offset,
239238
length,
@@ -301,10 +300,6 @@ Socket.prototype.send = function(buffer,
301300
if (ex) {
302301
if (callback) {
303302
callback(ex);
304-
305-
if (self.listeners('error').length)
306-
self.emit('error', ex);
307-
308303
return;
309304
}
310305

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)