Skip to content

Commit 38f1b4d

Browse files
authored
Shutdown executors on disable (#6565)
Closes #6446
1 parent 37ce087 commit 38f1b4d

3 files changed

Lines changed: 54 additions & 4 deletions

File tree

Essentials/src/main/java/com/earth2me/essentials/Essentials.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.earth2me.essentials.updatecheck.UpdateChecker;
4747
import com.earth2me.essentials.userstorage.ModernUserMap;
4848
import com.earth2me.essentials.utils.FormatUtil;
49+
import com.earth2me.essentials.utils.PasteUtil;
4950
import com.earth2me.essentials.utils.VersionUtil;
5051
import io.papermc.lib.PaperLib;
5152
import net.ess3.api.Economy;
@@ -587,6 +588,8 @@ public void onDisable() {
587588
getUsers().shutdown();
588589

589590
EssentialsConfiguration.shutdownExecutor();
591+
PasteUtil.shutdownExecutor();
592+
getServer().getScheduler().cancelTasks(this);
590593

591594
HandlerList.unregisterAll(this);
592595
}

Essentials/src/main/java/com/earth2me/essentials/I18n.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
import java.util.concurrent.ConcurrentHashMap;
2929
import java.util.concurrent.ExecutorService;
3030
import java.util.concurrent.Executors;
31+
import java.util.concurrent.TimeUnit;
3132
import java.util.function.Function;
3233
import java.util.logging.Level;
3334
import java.util.regex.Pattern;
3435

3536
public class I18n implements net.ess3.api.II18n {
3637
private static final String MESSAGES = "messages";
3738
private static final Pattern NODOUBLEMARK = Pattern.compile("''");
38-
private static final ExecutorService BUNDLE_LOADER_EXECUTOR = Executors.newFixedThreadPool(2);
39+
private static volatile ExecutorService BUNDLE_LOADER_EXECUTOR = Executors.newFixedThreadPool(2);
3940
private static final ResourceBundle NULL_BUNDLE = new ResourceBundle() {
4041
@SuppressWarnings("NullableProblems")
4142
public Enumeration<String> getKeys() {
@@ -106,6 +107,29 @@ public void onEnable() {
106107

107108
public void onDisable() {
108109
instance = null;
110+
shutdownExecutor();
111+
}
112+
113+
private static synchronized ExecutorService getExecutor() {
114+
if (BUNDLE_LOADER_EXECUTOR == null || BUNDLE_LOADER_EXECUTOR.isShutdown() || BUNDLE_LOADER_EXECUTOR.isTerminated()) {
115+
BUNDLE_LOADER_EXECUTOR = Executors.newFixedThreadPool(2);
116+
}
117+
return BUNDLE_LOADER_EXECUTOR;
118+
}
119+
120+
private static synchronized void shutdownExecutor() {
121+
final ExecutorService exec = BUNDLE_LOADER_EXECUTOR;
122+
if (exec == null) {
123+
return;
124+
}
125+
exec.shutdown();
126+
try {
127+
if (!exec.awaitTermination(5, TimeUnit.SECONDS)) {
128+
exec.shutdownNow();
129+
}
130+
} catch (final InterruptedException ignored) {
131+
exec.shutdownNow();
132+
}
109133
}
110134

111135
@Override
@@ -124,7 +148,7 @@ private ResourceBundle getBundle(final Locale locale) {
124148
synchronized (loadingBundles) {
125149
if (!loadingBundles.contains(locale)) {
126150
loadingBundles.add(locale);
127-
BUNDLE_LOADER_EXECUTOR.submit(() -> {
151+
getExecutor().submit(() -> {
128152
blockingLoadBundle(locale);
129153
synchronized (loadingBundles) {
130154
loadingBundles.remove(locale);

Essentials/src/main/java/com/earth2me/essentials/utils/PasteUtil.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import java.util.concurrent.CompletableFuture;
1717
import java.util.concurrent.ExecutorService;
1818
import java.util.concurrent.Executors;
19+
import java.util.concurrent.TimeUnit;
1920
import java.util.zip.GZIPOutputStream;
2021

2122
public final class PasteUtil {
2223
private static final String PASTE_URL = "https://pastes.dev/";
2324
private static final String PASTE_UPLOAD_URL = "https://api.pastes.dev/post";
24-
private static final ExecutorService PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
25+
private static volatile ExecutorService PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
2526
private static final Gson GSON = new Gson();
2627

2728
private PasteUtil() {
@@ -35,7 +36,7 @@ private PasteUtil() {
3536
*/
3637
public static CompletableFuture<PasteResult> createPaste(List<PasteFile> pages) {
3738
final CompletableFuture<PasteResult> future = new CompletableFuture<>();
38-
PASTE_EXECUTOR_SERVICE.submit(() -> {
39+
getExecutor().submit(() -> {
3940
try {
4041
final HttpURLConnection connection = (HttpURLConnection) new URL(PASTE_UPLOAD_URL).openConnection();
4142
connection.setRequestMethod("POST");
@@ -81,6 +82,28 @@ public static CompletableFuture<PasteResult> createPaste(List<PasteFile> pages)
8182
return future;
8283
}
8384

85+
private static synchronized ExecutorService getExecutor() {
86+
if (PASTE_EXECUTOR_SERVICE == null || PASTE_EXECUTOR_SERVICE.isShutdown() || PASTE_EXECUTOR_SERVICE.isTerminated()) {
87+
PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
88+
}
89+
return PASTE_EXECUTOR_SERVICE;
90+
}
91+
92+
public static synchronized void shutdownExecutor() {
93+
final ExecutorService exec = PASTE_EXECUTOR_SERVICE;
94+
if (exec == null) {
95+
return;
96+
}
97+
exec.shutdown();
98+
try {
99+
if (!exec.awaitTermination(5, TimeUnit.SECONDS)) {
100+
exec.shutdownNow();
101+
}
102+
} catch (final InterruptedException ignored) {
103+
exec.shutdownNow();
104+
}
105+
}
106+
84107
public static class PasteFile {
85108
private final String name;
86109
private final String contents;

0 commit comments

Comments
 (0)