Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch and emit error on socket.addMembership #34

Merged
merged 1 commit into from
Mar 7, 2017

Conversation

huanvu
Copy link
Contributor

@huanvu huanvu commented Sep 22, 2016

socket.addMembership throws an error on network issues (no network). This commit catches and emits the error so that the error can be handled elsewhere instead of crashing the library.

…ork connection) so that library doesn't crash
@pfrazee
Copy link

pfrazee commented Mar 6, 2017

We've hit this in Beaker (beakerbrowser/beaker#323). @mafintosh can you merge?

@mafintosh mafintosh merged commit fa643bd into mafintosh:master Mar 7, 2017
@mafintosh
Copy link
Owner

6.1.1

@pfrazee
Copy link

pfrazee commented Mar 7, 2017 via email

@thom-nic
Copy link

thom-nic commented Sep 14, 2017

Hey! I think this is what I was hitting in watson/bonjour#38 ❤️ Time to upgrade...

Edit: 💔 actually this patch was the cause of my problem. What you have now is a socket that's created, an emit('error') added and a bind() call all inside a constructor, so there's no way for me to reliably handle the error event... and an unhandled error event == process 💀

I can do the following:

const mdns = require('multicast-dns');
const myMdns = mdns(opts);
myMdns.on('error', err => console.warn("please don't crash my nodejs process"));

However I don't think there's any guarantee that my error handler will definitely be added before a socket.bind() emits an error. I think the proper way to handle this would for the constructor to return an instance with a bind() or listen() method, so you can do the following:

const myMdns = mdns(opts); // socket is not yet opened
myMdns.on('error', err => console.warn("please don't crash my nodejs process"));
myMdns.listen(); // now we can handle events

Obv that is a breaking API change, however.

See: https://nodejs.org/api/events.html#events_error_events
I hope I'm preaching to the choir when I say you need to be really careful about when and how you emit error events due to the fact they can crash your process (I see it done in two places in that constructor) and document that fact really well. I don't see any mention in the README that the returned instance can emit error events.

Happy to open a new issue to address this, just let me know and I'll move the discussion.

@thom-nic
Copy link

If we would like to fix this in a backward-compatible way, possibly:

const myMdns = mdns({autoOpen: false}); // socket will not be opened automatically, defaults to true for compat
myMdns.on('error', err => console.warn("please don't crash my nodejs process"));
myMdns.listen(); // new method to open the socket, if autoOpen: false option was given in ctor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants