-
Notifications
You must be signed in to change notification settings - Fork 7.3k
UDP throws unhandled error, even if callback is provided to send
.
#4846
Comments
Tested on v0.8.21, BTW. |
It's a backwards compatibility thing but I agree it's kind of quirky. I don't mind changing it for v0.10 but the thing is that the rules (i.e. what you have to keep in mind when you use the API) become more complex. Currently it works like this:
That would change to:
The actual change is pretty trivial though: diff --git a/lib/dgram.js b/lib/dgram.js
index 222de73..6787cff 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -264,7 +264,8 @@ Socket.prototype.send = function(buffer,
self._handle.lookup(address, function(err, ip) {
if (err) {
if (callback) callback(err);
- self.emit('error', err);
+ if (!callback || self.listeners('error').length > 0)
+ self.emit('error', err);
}
else if (self._handle) {
var req = self._handle.send(buffer, offset, length, port, ip); |
I obviously vote to change it. :) But, if it doesn't get changed, we should at least call this out explicitly in the documentation (if you have a callback, you also need an error handler, or else the whole app could die in a fire. :) The stack trace for the uncaught exception doesn't tell you anything about where it came from, so tracking this down in a big application with a lot of network IO is not much fun (as I can personally attest to. :P) |
Oh oh! While I'm on the subject, it would be super awesome if, instead of just "getaddrinfo ENOTFOUND", the exception had the name of the domain name that wasn't found in it. This is actually how I eventually tracked this down; by spinning a custom node.js that included the domain name it was looking up, so I had some kind of hint where this was coming from. :) |
I can also attest to the "tracking this down in a big application with a lot of network IO is not much fun". I have callback function to handle the error, but node crashes anyways since it calls a sys level error. Are there any plans to adopt this? |
I'd really rather not make the change that @bnoordhuis suggested above. The current behavior is consistent with Add an error event listener on the object. That's all there is to it. Sorry. I know it's wonky. If I could do it over again, I'd say that we just not pass the error to a callback in that way. and only have the @jwalton, your suggestion about putting the domain name on the exception is great, but needs to go in a separate issue: #5393 |
I'm reopening this issue. I've been thinking it over and I feel this needs a little more consideration. In particular:
That's true but streams and datagrams are different beasts. The former is a 1-on-1 communication channel, the latter one-to-many; you can't really compare them. The current API was put together without much thought and it shows. We won't be making radical changes but I think some cleanup is in order and this is one such wart. |
I totally agree with @bnoordhuis. As an example, take CoAP. It has all the mechanisms to reuse the same socket for multiple message exchanges with different hosts. I think there are two kind of errors: the ones related generally to the socket, and the ones related to the current message being sent. The general errors should go into the socket My actual solution is to filter the errors codes in the error handler, but it's bad and everybody in the very same situation will have the same problem. |
@bnoordhuis are there any plans to to this befor 0.12.x? |
@tjfontaine yay or nay for 0.12? |
I'm +1 on the idea, making sure we preserve the existing emission behavior for those with handlers installed |
The current workaround is this: https://github.com/mcollina/node-coap/blob/master/lib/agent.js#L54-L58. sock.on('error', function(err) {
// we are skipping DNS errors
if(err.code !== 'ENOTFOUND')
that.emit('error', err)
}) I think we cannot support it and stop emitting the error: it's good habit to have a listener on the error event of a socket. Here is the best behavior we can support:
|
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes nodejs#4846.
@bnoordhuis @indutny ... do we still want to do something with this one? |
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This is now fixed on the io.js |
Do we want to backport?
|
It is a semver major change, so if so, it needs to go into master. |
I'd say no backport then. We can pick it up later in the converged stream.
|
Works for me. |
Great!! |
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
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 <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
If you create a udp4 socket and send some data over it to an invalid hostname (as in the Coffee-Script example below), then the callback will be called to indicate that an error occurred. Unfortunately, the udp socket will ALSO throw a top-level exception which kills the entire app.
Sample:
Run with "coffee sample.coffee". You'll get the following output:
And the problem If you uncomment the two lines at the top, and define a "on error" handler, then the callback will be called with the error, and the error handler will also be called, and the program will not terminate (since the socket is never closed.)
I would expect that either the presence of a callback or a defined error handler would prevent killing the program, and not require both?
The text was updated successfully, but these errors were encountered: