Skip to content

Commit

Permalink
Handle synchronous socket.send error in sendUsingDnsCache (#271)
Browse files Browse the repository at this point in the history
* Handle synchronous socket.send error in sendUsingDnsCache

We are using UDP with DNS caching in production and noticed the occasional unhandled exception in our application when the datadog agent is unavailable. We tracked it down to the lookup branch of `sendUsingDnsCache` which calls `socket.send` within a callback. `socket.send` can throw a sync exception and not invoke `callback` as described here: https://nodejs.org/api/dgram.html#socketsendmsg-offset-length-port-address-callback.

* PR feedback
  • Loading branch information
matteosb authored Sep 13, 2024
1 parent 400d0ae commit 5b06c56
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ const createUdpTransport = args => {
}
dnsResolutionData.resolvedAddress = address;
dnsResolutionData.timestamp = now;
socket.send(buf, 0, buf.length, args.port, dnsResolutionData.resolvedAddress, callback);
try {
socket.send(buf, 0, buf.length, args.port, dnsResolutionData.resolvedAddress, callback);
} catch (socketError) {
callback(socketError);
}
});
} else {
socket.send(buf, 0, buf.length, args.port, dnsResolutionData.resolvedAddress, callback);
try {
socket.send(buf, 0, buf.length, args.port, dnsResolutionData.resolvedAddress, callback);
} catch (socketError) {
callback(socketError);
}
}
};

Expand All @@ -78,7 +86,11 @@ const createUdpTransport = args => {
if (args.cacheDns) {
sendUsingDnsCache(callback, buf);
} else {
socket.send(buf, 0, buf.length, args.port, args.host, callback);
try {
socket.send(buf, 0, buf.length, args.port, args.host, callback);
} catch (socketError) {
callback(socketError);
}
}
},
close: socket.close.bind(socket),
Expand Down

0 comments on commit 5b06c56

Please sign in to comment.