JDASocket unable to reconnect

Created on 21 Aug 2016  路  3Comments  路  Source: DV8FromTheWorld/JDA

It looks like my music bot got disconnected from the web socket, so it attempted to reconnect. However, when it does reconnect it throws this IllegalStateException and it disconnects. It looks like this in the log:

[17:08:19] [Warning] [JDASocket]: Got disconnected from WebSocket (Internet?!)... Attempting to reconnect in 2s
[17:08:21] [Warning] [JDASocket]: Attempting to reconnect!
[17:08:22] [Info] [JDASocket]: Connected to WebSocket
[17:08:22] [Fatal] [JDASocket]: Encountered an exception:
[17:08:22] [Fatal] [JDASocket]: java.lang.IllegalStateException: Already attempting to start an AudioConnection with a VoiceChannel!
Currently Attempting Channel ID: 204878762926407681  |  New Attempt Channel ID: 204878762926407681
    at net.dv8tion.jda.managers.impl.AudioManagerImpl.openAudioConnection(AudioManagerImpl.java:71)
    at net.dv8tion.jda.requests.WebSocketClient.reconnectAudioConnections(WebSocketClient.java:535)
    at net.dv8tion.jda.requests.WebSocketClient.ready(WebSocketClient.java:133)
    at net.dv8tion.jda.requests.WebSocketClient.handleEvent(WebSocketClient.java:584)
    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)

[17:08:22] [Warning] [JDASocket]: Got disconnected from WebSocket (Internet?!)... Attempting to reconnect in 2s
[17:08:24] [Warning] [JDASocket]: Attempting to reconnect!
[17:08:25] [Info] [JDASocket]: Connected to WebSocket
[etc]

This cycle would then repeat until I terminated my bot, but it was unusable until then. This is possibly related to #110.

bug

Most helpful comment

This as to do with the AudioWebsocket not closing properly / fast enough between the MainWS disconnect and subsequent reconnect process. When the MainWS disconnects it "saves" all known connected audio connections and tries to restore them when the MainWS reconnects to Discord.

Thus, if an audio connection doesn't actually die, or it isn't cleared in time, it looks like you already have an open connection.

When I recode the audio connecting system in 3.0 I'll be sure to fix this.

All 3 comments

This as to do with the AudioWebsocket not closing properly / fast enough between the MainWS disconnect and subsequent reconnect process. When the MainWS disconnects it "saves" all known connected audio connections and tries to restore them when the MainWS reconnects to Discord.

Thus, if an audio connection doesn't actually die, or it isn't cleared in time, it looks like you already have an open connection.

When I recode the audio connecting system in 3.0 I'll be sure to fix this.

So my previous answer actually wasn't correct. This was caused by the AudioWebSocket closing before the MainWS. This meant that it attempted to reconnect immediately which involves sending data over the MainWS. However, the MainWS was probably also disconnected but it hadn't noticed yet. So the data sent to discord was never received.

This meant that JDA throught it had attempted to establish a connection to discord, and was expecting a response, but would never receive one. When the MainWS finally disconnected, it recorded that the audio connection had disconnected due to error (probably along with a ton of other audio connections). These connections are recorded and cached to be reestablished when the MainWS reconnected.

So, when the MainWS reconnected, it attempted to reestablish all audio connections, but the one in the stack trace had already been attempted, so the internals of JDA were out of sync with reality, thus you see the error shown.

Additionally, 2.x had a problem with easily killing the MainWS due to sending too many WS messages too quickly when reestablishing audio connections after a reconnect, especially when there were a ton of audio connections that needed to be reestablished.

Both problems will be addressed when audio finally releases to development. The last problem mentioned has already been addressed.

Reconnect killing Websocket due to too many messages; Fixed: 5f2bd4308c0c16d4827a4d1a9c59ef225fa8287e

Audio connections failing to reconnect, invalidating cache; Fixed: a97afac2c0bad4320998caad4f90d5465af62b89

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stefaandevylder picture stefaandevylder  路  5Comments

hitsu420 picture hitsu420  路  6Comments

Andre601 picture Andre601  路  6Comments

istamendil picture istamendil  路  3Comments

Bungeefan picture Bungeefan  路  6Comments