Skip to content

Commit

Permalink
fix: make sure that Minestom services are properly shutting down
Browse files Browse the repository at this point in the history
Unlike CraftBukkit, Minestom does not call System.exit() when shutting down.
Minestom relies on the outside process to release all resources and non-daemon threads so that the process can properly exit.

For reference see net.minecraft.server.dedicated.DedicatedServer#onServerExit which is being called by MinecraftServer#halt which is in turn being handled by MinecraftServer#runServer (second nested finally).
  • Loading branch information
GiantTreeLP committed Nov 25, 2023
1 parent fc24878 commit d1de3c4
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package eu.cloudnetservice.modules.bridge.platform.minestom;

import eu.cloudnetservice.common.concurrent.Task;
import eu.cloudnetservice.driver.network.NetworkClient;
import eu.cloudnetservice.driver.registry.ServiceRegistry;
import eu.cloudnetservice.driver.util.ModuleHelper;
import eu.cloudnetservice.ext.platforminject.api.PlatformEntrypoint;
Expand Down Expand Up @@ -49,19 +51,22 @@ public final class MinestomBridgeExtension implements PlatformEntrypoint {
private final ModuleHelper moduleHelper;
private final ServiceRegistry serviceRegistry;
private final MinestomBridgeManagement bridgeManagement;
private final NetworkClient networkClient;

@Inject
public MinestomBridgeExtension(
@NonNull ComponentLogger logger,
@NonNull ModuleHelper moduleHelper,
@NonNull ServiceRegistry serviceRegistry,
@NonNull MinestomBridgeManagement bridgeManagement,
@NonNull MinestomPlayerManagementListener playerListener
@NonNull MinestomPlayerManagementListener playerListener,
@NonNull NetworkClient networkClient
) {
this.logger = logger;
this.moduleHelper = moduleHelper;
this.serviceRegistry = serviceRegistry;
this.bridgeManagement = bridgeManagement;
this.networkClient = networkClient;
}

@Override
Expand All @@ -84,5 +89,15 @@ public void onLoad() {
@Override
public void onDisable() {
this.moduleHelper.unregisterAll(this.getClass().getClassLoader());
try {
this.networkClient.close();
} catch (Exception exception) {
this.logger.error("Exception while closing network client", exception);
}
try {
Task.shutdownNow();
} catch (InterruptedException exception) {
this.logger.error("Exception while shutting down task executor", exception);
}
}
}

0 comments on commit d1de3c4

Please sign in to comment.