Skip to content

Commit

Permalink
Merge pull request #24 from granny/fix/interruptedexception-when-shut…
Browse files Browse the repository at this point in the history
…ting-down

Fix InterruptedException when shutting down CLH or JDA & Port JDA4 fixes to JDA5
  • Loading branch information
Scarsz authored Jul 7, 2024
2 parents 84b88d6 + 6a69316 commit 44ca62f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private Message updateMessage() throws IllegalStateException {
}
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
Expand All @@ -264,7 +264,7 @@ private Message updateMessage() throws IllegalStateException {
throw new RuntimeException(e);
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
Expand Down Expand Up @@ -302,11 +302,16 @@ public void recreateChannel(@Nullable String reason) {
*/
public void shutdownExecutor() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
scheduledFuture.cancel(false);
scheduledFuture = null;
}
if (executor != null) {
executor.shutdown();
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore, it's fine if the last tidbit of console output doesn't get sent
}
executor = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,39 @@ private Message updateMessage() throws IllegalStateException {

if (currentMessage != null) {
try {
return currentMessage.editMessage(full).complete();
} catch (ErrorResponseException e) {
if (e.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
return currentMessage.editMessage(full).submit().get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof ErrorResponseException
&& ((ErrorResponseException) cause).getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
} else {
throw e;
throw new RuntimeException(cause);
}
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
throw new RuntimeException(e);
}
}
}

return channel.sendMessage(full).complete();
try {
return channel.sendMessage(full).submit().get();
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
throw new RuntimeException(e);
}
}
}

/**
Expand Down Expand Up @@ -280,11 +302,16 @@ public void recreateChannel(@Nullable String reason) {
*/
public void shutdownExecutor() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
scheduledFuture.cancel(false);
scheduledFuture = null;
}
if (executor != null) {
executor.shutdown();
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore, it's fine if the last tidbit of console output doesn't get sent
}
executor = null;
}
}
Expand Down

0 comments on commit 44ca62f

Please sign in to comment.