-
Notifications
You must be signed in to change notification settings - Fork 7.3k
dgram socket.send() throws exceptions instead of invoking callback on DNS lookup failure #2900
Comments
This is being seen in v0.6.10. |
A workaround for #916 / nodejs/node-v0.x-archive#2900. An infelicity in dgram.js means that DNS lookup failures on datagram sends will result in an exception getting thrown, instead of the exception being trapped and passed off to the send's callback.
Actually, I spoke too soon -- because the exception is getting thrown on the next tick, there's no way to trap it except at a global level. Is there a supported pattern for dealing with this, or is this a straight-up bug? |
...even if you're only going to send packets off into the void. Correctly deals with #916 and nodejs/node-v0.x-archive#2900, although it should be sufficient just to pass an error handler to socket.send().
Further investigation reveals that even if you're using the dgram socket as a send-only connection, you still need to have an error handler defined on the socket listener. Even if there's an error handler on socket.send() (as in this example), if there's not an error listener on the socket, you'll get an unhandled exception propagated to the root. This begs the question of why socket.send() accepts an error handler. |
Looks like someone forgot to change it over from |
That call to
It's not an error handler, it's a 'datagram sent' callback. The Are you seeing actual bugs? If so, can you post a test case? |
Test case can be seen in the example linked above, based on code from the dgram documentation:
To be clear, this might not be a bug, but this behavior differs from what I would have expected given the documentation. |
Right, the exception object is passed to the callback and emitted as an |
Don't know that I would want to stop it from emitting and using a callback. They could be using the event for generic logging, and the callback to handle and generate specific error types based on the |
These points make sense, but it would be very useful if there were a single-call method that would manage setting up and tearing down the connection for just shooting off single packets into the network. I'm using UDP for statsd instrumentation, and it doesn't require the full overhead of a persistent socket connection. I can guarantee that the connection will never receive any packets. |
I'm hitting this issue in https://github.com/mcollina/node-coap, again in node v0.10.20. To reproduce: var dgram = require('dgram')
, client = dgram.createSocket('udp4')
client.send(new Buffer(1), 0, 1, 1234, 'aaaa.eee', function(err) {
if (err)
console.log('error received')
client.close()
}) This unfortunately result in:
|
Adding an This very fact make the whole No real issue, but the doc should come with a huge warning on that, something like: |
A workaround for LockerProject/Locker#916 / nodejs/node-v0.x-archive#2900. An infelicity in dgram.js means that DNS lookup failures on datagram sends will result in an exception getting thrown, instead of the exception being trapped and passed off to the send's callback.
...even if you're only going to send packets off into the void. Correctly deals with #916 and nodejs/node-v0.x-archive#2900, although it should be sufficient just to pass an error handler to socket.send().
For context, see LockerProject/Locker#916.
Looking at lines 146-148 of dns.js, it seems like should the c-ares call fail, the lookup code will throw an exception that isn't trapped and passed off to the callback in dgram.js. This means that right now, to completely handle some expected failure modes, it's necessary to wrap the call (which already has a callback ready to handle errors) in an exception-handling block.
The text was updated successfully, but these errors were encountered: