Skip to content

Commit acac161

Browse files
mcollinarvagg
authored andcommitted
dgram: make send cb act as "error" event handler
Modifies the dgram send() method to not emit errors when a DNS lookup fails if there is a 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 on nodejs/node-v0.x-archive#7738, which landed in 77266d7. Fixes: nodejs/node-v0.x-archive#4846 Refs: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 6b3f046 commit acac161

File tree

4 files changed

+41
-62
lines changed

4 files changed

+41
-62
lines changed

doc/api/dgram.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ assigned a random port number and is bound to the "all interfaces" address
114114
An optional callback may be specified to detect DNS errors or for determining
115115
when it's safe to reuse the `buf` object. Note that DNS lookups delay the time
116116
to send for at least one tick. The only way to know for sure that the datagram
117-
has been sent is by using a callback. If an error occurs and a callback is given,
118-
the error will be the first argument to the callback. If a callback is not given,
119-
the error is emitted as an `'error'` event on the `socket` object.
117+
has been sent is by using a callback. If an error occurs and a callback is
118+
given, the error will be the first argument to the callback. If a callback is
119+
not given, the error is emitted as an `'error'` event on the `socket` object.
120120

121121
With consideration for multi-byte characters, `offset` and `length` will
122122
be calculated with respect to

lib/dgram.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,8 @@ Socket.prototype.send = function(buffer,
300300

301301
self._handle.lookup(address, function(ex, ip) {
302302
if (ex) {
303-
if (callback) {
303+
if (typeof callback === 'function') {
304304
callback(ex);
305-
306-
if (self.listeners('error').length)
307-
self.emit('error', ex);
308-
309305
return;
310306
}
311307

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

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

-54
This file was deleted.

0 commit comments

Comments
 (0)