gizmo385 / discord.clj

A Clojure library for creating Discord bots
MIT License
45 stars 14 forks source link

Disconnection causes program to hang #18

Closed DrAtomic closed 4 years ago

DrAtomic commented 4 years ago

The server that hosts my bot that uses your library often disconnects. This causes an error above 1001. Right now the bot doesn't reconnect or exit the program. https://github.com/gizmo385/discord.clj/blob/master/src/discord/gateway.clj#L258 which causes my bot to hang and then i have to log into my server and restart it manually.

WARN [discord.gateway:259] - Closing Gateway websocket, not reconnecting (1006).
Exception in thread "async-dispatch-7" org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
    at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:299)
    at gniazdo.core$connect_with_client$reify__11265.send_msg(core.clj:138)
    at discord.gateway.DiscordGateway.send_message(gateway.clj:62)
    at discord.gateway$send_heartbeat.invokeStatic(gateway.clj:204)
    at discord.gateway$send_heartbeat.invoke(gateway.clj:202)
    at discord.gateway$connect_to_gateway$fn__15913$state_machine__5591__auto____15932$fn__15934.invoke(gateway.clj:292)
    at discord.gateway$connect_to_gateway$fn__15913$state_machine__5591__auto____15932.invoke(gateway.clj:292)
    at clojure.core.async.impl.ioc_macros$run_state_machine.invokeStatic(ioc_macros.clj:973)
    at clojure.core.async.impl.ioc_macros$run_state_machine.invoke(ioc_macros.clj:972)
    at clojure.core.async.impl.ioc_macros$run_state_machine_wrapped.invokeStatic(ioc_macros.clj:977)
    at clojure.core.async.impl.ioc_macros$run_state_machine_wrapped.invoke(ioc_macros.clj:975)
    at clojure.core.async$ioc_alts_BANG_$fn__5794.invoke(async.clj:384)
    at clojure.core.async$do_alts$fn__5740$fn__5743.invoke(async.clj:253)
    at clojure.core.async.impl.channels.ManyToManyChannel$fn__509.invoke(channels.clj:265)
    at clojure.lang.AFn.run(AFn.java:22)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
^C

I made the changes

(if (> 1001 status)
                      (do
                        (timbre/warnf "Socket closed for unexpected reason (%d): %s" status reason)
                        (timbre/warnf "Attempting to reconnect to websocket...")
                        (reconnect-gateway gateway))
                      (do (timbre/warnf "Closing Gateway websocket, not reconnecting (%d)." status)
                          (System/exit 1)))

which causes the program to exit if an abnormal error occurs.

WARN [discord.gateway:259] - Closing Gateway websocket, not reconnecting (1006).
Exception in thread "async-dispatch-6" org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
    at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:299)
    at gniazdo.core$connect_with_client$reify__11265.send_msg(core.clj:138)
    at discord.gateway.DiscordGateway.send_message(gateway.clj:62)
    at discord.gateway$send_heartbeat.invokeStatic(gateway.clj:204)
    at discord.gateway$send_heartbeat.invoke(gateway.clj:202)
    at discord.gateway$connect_to_gateway$fn__15913$state_machine__5591__auto____15932$fn__15934.invoke(gateway.clj:293)
    at discord.gateway$connect_to_gateway$fn__15913$state_machine__5591__auto____15932.invoke(gateway.clj:293)
    at clojure.core.async.impl.ioc_macros$run_state_machine.invokeStatic(ioc_macros.clj:973)
    at clojure.core.async.impl.ioc_macros$run_state_machine.invoke(ioc_macros.clj:972)
    at clojure.core.async.impl.ioc_macros$run_state_machine_wrapped.invokeStatic(ioc_macros.clj:977)
    at clojure.core.async.impl.ioc_macros$run_state_machine_wrapped.invoke(ioc_macros.clj:975)
    at clojure.core.async$ioc_alts_BANG_$fn__5794.invoke(async.clj:384)
    at clojure.core.async$do_alts$fn__5740$fn__5743.invoke(async.clj:253)
    at clojure.core.async.impl.channels.ManyToManyChannel$fn__509.invoke(channels.clj:265)
    at clojure.lang.AFn.run(AFn.java:22)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
2020-04-03 22:42:58.216:WARN:oejut.QueuedThreadPool:Thread-2: WebSocketClient@282234594{STOPPING,8<=8<=200,i=0,q=1} Couldn't stop Thread[WebSocketClient@282234594-13,5,main]

is it all right if i make a PR?

gizmo385 commented 4 years ago

@DrAtomic Definitely! PRs are always welcome! 👍