Jda: Connection reset when attempting to join voice channel

Created on 20 Aug 2016  路  1Comment  路  Source: DV8FromTheWorld/JDA

I'm getting this exception sometimes when someone requests my music bot to join a voice channel:

[21:31:55] [Fatal] [JDASocket]: Encountered an exception:
[21:31:55] [Fatal] [JDASocket]: java.lang.RuntimeException: com.neovisionaries.ws.client.WebSocketException: Failed to send an opening handshake request to the server: Connection reset
        at net.dv8tion.jda.audio.AudioWebSocket.<init>(AudioWebSocket.java:109)
        at net.dv8tion.jda.handle.VoiceServerUpdateHandler.handleInternally(VoiceServerUpdateHandler.java:68)
        at net.dv8tion.jda.handle.SocketHandler.handle(SocketHandler.java:38)
        at net.dv8tion.jda.requests.WebSocketClient.handleEvent(WebSocketClient.java:615)
        at net.dv8tion.jda.requests.WebSocketClient.onTextMessage(WebSocketClient.java:324)
        at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
        at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:233)
        at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:211)
        at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:910)
        at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:693)
        at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:102)
        at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:61)
Caused by: com.neovisionaries.ws.client.WebSocketException: Failed to send an opening handshake request to the server: Connection reset
        at com.neovisionaries.ws.client.WebSocket.writeHandshake(WebSocket.java:2546)
        at com.neovisionaries.ws.client.WebSocket.shakeHands(WebSocket.java:2437)
        at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:1583)
        at net.dv8tion.jda.audio.AudioWebSocket.<init>(AudioWebSocket.java:104)
        ... 11 more
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:209)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
        at sun.security.ssl.InputRecord.read(InputRecord.java:503)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
        at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)
        at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
        at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
        at java.io.FilterOutputStream.flush(FilterOutputStream.java:140)
        at com.neovisionaries.ws.client.WebSocket.writeHandshake(WebSocket.java:2539)
        ... 14 more

As a result of this the bot doesn't actually connect to the voice channel, but the audio manager will still think it is trying to connect to the voice channel, since it throws this exception the second time:

java.lang.IllegalStateException: Already attempting to start an AudioConnection with a VoiceChannel!
Currently Attempting Channel ID: 99619915127848960  |  New Attempt Channel ID: 99619915127848960
        net.dv8tion.jda.managers.impl.AudioManagerImpl.openAudioConnection(AudioManagerImpl.java:71)
        fredboat.audio.GuildPlayer.joinChannel(GuildPlayer.java:70)
        fredboat.audio.GuildPlayer.joinChannel(GuildPlayer.java:47)
        fredboat.command.music.JoinCommand.onInvoke(JoinCommand.java:17)
        fredboat.commandmeta.CommandManager.prefixCalled(CommandManager.java:39)

I have also noticed that sometimes the bot will appear to be connected to a voice channel on the client but is unable to play audio or closing the connection with AudioManager.closeAudioConnection(), having no effect at all. In this case trying to connect to the voice channel results in the same IllegalStateException.

Restarting the bot seems to be the only temporary fix, as kicking and reinviting the bot will not fix it.
It might be that the WebSocketException is only triggered after the bug has first occurred and the bot is reinvited.

This has been going on for several versions now. The bot is currently running JDA 2.2.1_355.

bug

Most helpful comment

The has to do with JDA not being able to establish the connection. An audio connection is a 3 part system.

1) We tell Discord we are joining which immediately puts us in the room visually on the client.

2) Discord sends us information to connect. Sometimes, if Discord does not have enough resources, it will send us a null endpoint which is a promise that Discord will send a valid endpoint later to connect with. JDA currently doesn't handle this.

3) After getting the info, JDA attempts to setup the audio connection. We first open a Websocket connection, send identifying information and then attempt to connect a UDP socket for audio connection.

The above error occurs in step 3 when JDA attempts to open a Websocket connection to discord. Discord is ignoring our Websocket's attempt at a connection handshake, thus, things are failing. Not quite sure why. My guess would be that you briefly have a loss of internet when the socket is opening, thus making the request never reach Discord. A possible fix would be to retry establishing the connection similarly to the past fix for the UDP socket not connecting.

The issue with the AudioManager believing it is in the channel is because JDA expects things to go well and, in this situation, doesn't fully handle the possibilities of failure on connection. I'll look into implementing a fix.

>All comments

The has to do with JDA not being able to establish the connection. An audio connection is a 3 part system.

1) We tell Discord we are joining which immediately puts us in the room visually on the client.

2) Discord sends us information to connect. Sometimes, if Discord does not have enough resources, it will send us a null endpoint which is a promise that Discord will send a valid endpoint later to connect with. JDA currently doesn't handle this.

3) After getting the info, JDA attempts to setup the audio connection. We first open a Websocket connection, send identifying information and then attempt to connect a UDP socket for audio connection.

The above error occurs in step 3 when JDA attempts to open a Websocket connection to discord. Discord is ignoring our Websocket's attempt at a connection handshake, thus, things are failing. Not quite sure why. My guess would be that you briefly have a loss of internet when the socket is opening, thus making the request never reach Discord. A possible fix would be to retry establishing the connection similarly to the past fix for the UDP socket not connecting.

The issue with the AudioManager believing it is in the channel is because JDA expects things to go well and, in this situation, doesn't fully handle the possibilities of failure on connection. I'll look into implementing a fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StijnSimons picture StijnSimons  路  3Comments

sachinhasaphatass picture sachinhasaphatass  路  3Comments

AutismSpeaks picture AutismSpeaks  路  4Comments

Frederikam picture Frederikam  路  4Comments

napstr picture napstr  路  3Comments