discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Network Failure Unhandled #1677

Closed ChrisTalman closed 6 years ago

ChrisTalman commented 7 years ago

Please describe the problem you are having in as much detail as possible: An unhandled exception, as follows, is thrown when my network adaptor is disabled. This seems odd, as I was under the impression that this should be handled gracefully by the disconnect event.

Error: getaddrinfo ENOENT gateway.discord.gg:443
    at Object.exports._errnoException (util.js:1034:11)
    at errnoException (dns.js:33:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26)

Include a reproducible code sample here, if possible: The exception is thrown whenever there is any active connection to the Discord API, such as below.

const Discord = require('discord.js');
let robot = new Discord.Client();
let token = 'example123';
robot.login(token);

Further details:

devsnek commented 7 years ago

this is your internet, not a lib problem

ChrisTalman commented 7 years ago

@devsnek Well, in this case, my Internet was fine, and I deliberately disabled my network adaptor to see whether a loss of connection would be handled gracefully. It was not. Strictly speaking, is this not what the disconnect event is intended to handle? Its description is as follows: 'Emitted when the client's WebSocket disconnects'. Is this not a case of the client's WebSocket disconnecting?

devsnek commented 7 years ago

it only emits when the socket disconnects in a way that it can't reconnect, usually we will automatically resume the ws for you.

ChrisTalman commented 7 years ago

@devsnek Okay. Well, this seems to be such a case, where the socket cannot reconnect. It would be nice for Discord.js to gracefully handle an exception such as this. It seems to fall within the scope of the disconnect event as described in the documentation, and would be very helpful in a library which handles WebSockets at its core.

ghost commented 7 years ago

@ChrisTalman How exactly would you handle such an exception? If you can't get DNS info about an address, then you can't reconnect. The only thing you can do at this point is to shut down the process.

ChrisTalman commented 7 years ago

@CharlotteDunois The context is that a network connection is not currently possible, but that it could be in the future. Thus, it is possible to reconnect, eventually. The library could handle this itself by reconnecting automatically, or application code could handle disconnect and call login at some interval.

ShadowDrakken commented 7 years ago

This error happens when internet connectivity is lost by any means, not just manually disabling the NIC. Previously, the bot would sit there and wait and if the connection returned within about 2 minutes it would reconnect. Well, sometimes, it's always been flakey about reconnecting.

Anyway, the correct behavior would be to sit and wait, occasionally polling to see if it can reconnect. Say once per minute or something to that effect. Not to just dump out and give up without the user's interaction.

devsnek commented 7 years ago

I disagree, we aren't responsible for your internet. If you need to keep something hosted 24/7 buy yourself some hosting or get better internet.

ShadowDrakken commented 7 years ago

That's not what the issue is about. It's about gracefully handling network interruptions, like every other half decent piece of software.

devsnek commented 7 years ago

we will not keep a thread open "occasionally polling to see if it can reconnect". that is objectively stupid. you can poll yourself and call login if you need to.

ChrisTalman commented 7 years ago

If that's the behaviour the user wants, I don't see how that's 'objectively stupid'. It's hardly like that would be an intensive operation, and it could be made optional. But, in any case, this issue started with the even more basic expectation that the disconnect event be fired in the case of a network failure instead of an exception being thrown. If this were done, a user could implement their own reconnection attempts. Although, having said this, having reconnection functionality out of the box would clearly be valuable to some users as has been demonstrated by this issue thread.

ShadowDrakken commented 7 years ago

"You can poll yourself and call login"

That it patently untrue, because the exception is closing the process out leaving no opportunities to do this.

ShadowDrakken commented 7 years ago

ChrisTalman: it used to have auto-reconnect. Prior to this error developing.

devsnek commented 7 years ago

the option known as autoReconnect was removed in 11.x because we made it so you cannot turn it off. The issue with the error not bubbling up through client#onerror and client#ondisconnect will be fixed, but no new methods for "extended" reconnection will be added.

aemino commented 7 years ago

Personally, I think that the library should be tolerant of various network faults. If no one else bothers to implement this, I will eventually.

amishshah commented 7 years ago

Mostly fixed in master, may still error on failed reconnects (not sure can't test atm)

SpaceEEC commented 6 years ago

Is this still an issue? Simply handling the error event with a console.log and letting discord.js reconnecting is working fine for me on 11.3 and https://github.com/discordjs/discord.js/commit/5352e28700e51f1148463f350fe5694124a3c270.

ChrisTalman commented 6 years ago

I'm not sure. I didn't see this issue mentioned in the release notes for 11.3.

I certainly know that socket hang exceptions still seem to happen (for instance, see #2047). This may or may not be related to the network failure handling, but this is difficult to determine given that in both cases the exception is thrown out of nowhere. Both certainly concern appropriately handling failure on the network, though.

kyranet commented 6 years ago

After voice-rewrite, it seems to be fixed. The commit from the master branch in 5efddac (before voice-rewrite) doesn't handle these errors, and oddly enough, it logs the ECONNRESET twice: one as handled, and another as unhandled.

After 86da7af, that got fixed in my end and these errors are properly handled by the error event.

Lewdcario commented 6 years ago

This is not an issue on 11.3-dev, so I will close this.