rmcafee / discord_ex

Discord Elixir Library
MIT License
47 stars 13 forks source link

SSL Error #32

Closed tielur closed 6 years ago

tielur commented 7 years ago

Is anyone else getting these errors? They seem to continuously happen, I haven't timed exactly how far a part they happen though. I've replaced what I thought was sensitive information with REMOVED. It looks like it's something with the websocket library maybe?

17:23:52.556 [error] ** State machine #PID<0.1098.0> terminating 
** Last message in was {:ssl_closed,
 {:sslsocket, {:gen_tcp, #Port<0.11004>, :tls_connection, :undefined},
  #PID<0.1100.0>}}
** When State == :connected
**      Data  == {:context,
 {:websocket_req, :wss, 'gateway.discord.gg', 443, '/?v=4&encoding=etf',
  :infinity, :undefined, 1,
  {:sslsocket, {:gen_tcp, #Port<0.11004>, :tls_connection, :undefined},
   #PID<0.1100.0>},
  {:transport, :ssl, :ssl, :ssl_closed, :ssl_error,
   [mode: :binary, active: true, verify: :verify_none, packet: 0]},
  "REMOVED", :undefined, 1, 2, :undefined, :undefined},
 {:transport, :ssl, :ssl, :ssl_closed, :ssl_error,
  [mode: :binary, active: true, verify: :verify_none, packet: 0]}, [],
 {:wss, 'gateway.discord.gg', 443, '/?v=4&encoding=etf'},
 {DiscordEx.Client,
  %{agent_seq_num: #PID<0.1099.0>, client_id: REMOVED,
    client_pid: #PID<0.1098.0>,
    guilds: [%{guild_id: REMOVED, voice_states: []}],
    handler: Albionbot.Bot, rest_client: #PID<0.1097.0>,
    session_id: "b664e142fa6cd8c6ace0daa4d1a0c91b",
    token: "Bot REMOVED"}},
 "", false, 0}
** Reason for termination = 
** {{:case_clause, nil},
 [{:websocket_client, :disconnect, 2,
   [file: '/Users/tielur/sites/albionbot/deps/websocket_client/src/websocket_client.erl',
    line: 260]}, {:gen_fsm, :handle_msg, 7, [file: 'gen_fsm.erl', line: 451]},
  {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]}
jussih commented 7 years ago

I'm getting the same error, the websocket is often closed after only 2 minutes of uptime. The stacktrace you are mentioning is actually because the ondisconnect handler in discord_ex does not have a return value and the websocket behaviour is expecting one. If you change the ondisconnect thusly:

  def ondisconnect({:remote, :closed}, _state) do
    # Stub for beter actions later
    {:close, {:remote, :closed}, _state}
  end

You get the intended reaction and the websocket library wraps itself up neatly and terminates. This does not lead anywhere though, the end result is still the same, the connection is dead :)

Discord documentation states that when the server side disconnects the socket it returns an error code telling why it did so (https://discordapp.com/developers/docs/topics/gateway#disconnections) but this information is lost because the websocket library does not handle it: https://github.com/sanmiguel/websocket_client/issues/48

To get to the actual reason for disconnections, we'd need the information in the close code when/if discord is deciding to cut the socket.

jussih commented 7 years ago

I did a quick hack to update discord_ex to use discord's gateway protocol version 6, and in a couple of trials there were no more disconnects. Protocol version 4 which is currently used is deprecated and going to be removed on October 16th 2017, so the upgrade is needed anyway.

tielur commented 7 years ago

@jussih Do you have a fork with these changes?

jussih commented 7 years ago

@tielur https://github.com/jussih/discord_ex/tree/apiv6

The branch implements the bare minimum required to use the v6 protocol. It would be possible to detect a stale websocket due to the heartbeat ACK messages and reconnect and resume automatically, but I haven't implemented that.

jussih commented 6 years ago

This should be fixed now that https://github.com/rmcafee/discord_ex/pull/43 is merged.

tielur commented 6 years ago

I haven't seen this error in awhile, I'll reopen if it becomes an issue.