Fix non-daemon command manager thread sometimes preventing shutdown

This commit is contained in:
Luck 2021-01-16 18:24:10 +00:00
parent 73230bc9b6
commit 2a44572fb2
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -72,8 +72,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.Executors;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -83,16 +82,13 @@ import java.util.stream.Collectors;
public class CommandManager { public class CommandManager {
private final LuckPermsPlugin plugin; private final LuckPermsPlugin plugin;
private final ReentrantLock lock;
// the default executor to run commands on
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final TabCompletions tabCompletions; private final TabCompletions tabCompletions;
private final Map<String, Command<?>> mainCommands; private final Map<String, Command<?>> mainCommands;
public CommandManager(LuckPermsPlugin plugin) { public CommandManager(LuckPermsPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
this.lock = new ReentrantLock(true); // enable fairness
this.tabCompletions = new TabCompletions(plugin); this.tabCompletions = new TabCompletions(plugin);
this.mainCommands = ImmutableList.<Command<?>>builder() this.mainCommands = ImmutableList.<Command<?>>builder()
.add(new UserParentCommand()) .add(new UserParentCommand())
@ -134,13 +130,16 @@ public class CommandManager {
public CompletableFuture<CommandResult> executeCommand(Sender sender, String label, List<String> args) { public CompletableFuture<CommandResult> executeCommand(Sender sender, String label, List<String> args) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
this.lock.lock();
try { try {
return execute(sender, label, args); return execute(sender, label, args);
} catch (Throwable e) { } catch (Throwable e) {
this.plugin.getLogger().severe("Exception whilst executing command: " + args, e); this.plugin.getLogger().severe("Exception whilst executing command: " + args, e);
return null; return null;
} finally {
this.lock.unlock();
} }
}, this.executor); }, this.plugin.getBootstrap().getScheduler().async());
} }
public boolean hasPermissionForAny(Sender sender) { public boolean hasPermissionForAny(Sender sender) {