Skip to content

Commit

Permalink
Fix cleaning up of the keepAliveRunnable (#381)
Browse files Browse the repository at this point in the history
(cherry picked from commit b159de8)
  • Loading branch information
Shredder121 authored and MinnDevelopment committed Jun 21, 2017
1 parent 4abd26a commit 69efe74
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/main/java/net/dv8tion/jda/core/audio/AudioWebSocket.java
Expand Up @@ -35,10 +35,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class AudioWebSocket extends WebSocketAdapter
Expand All @@ -64,8 +61,8 @@ public class AudioWebSocket extends WebSocketAdapter
private final String token;
private boolean connected = false;
private boolean ready = false;
private boolean shutdown;
private Runnable keepAliveRunnable;
private boolean shutdown = false;
private Future<?> keepAliveHandle;
private String wssEndpoint;
private boolean shouldReconnect;

Expand Down Expand Up @@ -223,7 +220,6 @@ public void onTextMessage(WebSocket websocket, String message)
}
}


@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer)
{
Expand Down Expand Up @@ -331,10 +327,10 @@ public void close(ConnectionStatus closeStatus)
);
api.getClient().send(obj.toString());
}
if (keepAliveRunnable != null)
if (keepAliveHandle != null)
{
keepAlivePool.remove(keepAliveRunnable);
keepAliveRunnable = null;
keepAliveHandle.cancel(false);
keepAliveHandle = null;
}

if (audioConnection != null)
Expand Down Expand Up @@ -476,10 +472,10 @@ private InetSocketAddress handleUdpDiscovery(InetSocketAddress address, int ssrc

private void setupKeepAlive(final int keepAliveInterval)
{
if (keepAliveRunnable != null)
if (keepAliveHandle != null)
LOG.fatal("Setting up a KeepAlive runnable while the previous one seems to still be active!!");

keepAliveRunnable = () ->
Runnable keepAliveRunnable = () ->
{
if (socket.isOpen() && socket.isOpen() && !udpSocket.isClosed())
{
Expand Down Expand Up @@ -514,7 +510,7 @@ private void setupKeepAlive(final int keepAliveInterval)

try
{
keepAlivePool.scheduleAtFixedRate(keepAliveRunnable, 0, keepAliveInterval, TimeUnit.MILLISECONDS);
keepAliveHandle = keepAlivePool.scheduleAtFixedRate(keepAliveRunnable, 0, keepAliveInterval, TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ignored) {} //ignored because this is probably caused due to a race condition
// related to the threadpool shutdown.
Expand Down

0 comments on commit 69efe74

Please sign in to comment.