From fa9d5f6499f6265aada3a207bed11c7f456614b5 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Wed, 6 Mar 2019 22:51:39 -0500 Subject: [PATCH] Fire ProxyShutdownEvent _after_ the event manager thread pool shuts down Fixes #177 --- .../velocitypowered/proxy/VelocityServer.java | 3 ++- .../proxy/plugin/VelocityEventManager.java | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 81987889b..b087a71da 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -356,7 +356,6 @@ public class VelocityServer implements ProxyServer { this.cm.shutdown(); - eventManager.fire(new ProxyShutdownEvent()); try { if (!eventManager.shutdown() || !scheduler.shutdown()) { logger.error("Your plugins took over 10 seconds to shut down."); @@ -366,6 +365,8 @@ public class VelocityServer implements ProxyServer { Thread.currentThread().interrupt(); } + eventManager.fireShutdownEvent(); + shutdown = true; if (explicitExit) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index 024d80a13..7badf9ccb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -8,6 +8,7 @@ import com.velocitypowered.api.event.EventHandler; import com.velocitypowered.api.event.EventManager; import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.PluginManager; import java.lang.reflect.Method; import java.net.URL; @@ -121,19 +122,23 @@ public class VelocityEventManager implements EventManager { CompletableFuture eventFuture = new CompletableFuture<>(); service.execute(() -> { - PostResult result = bus.post(event); - if (!result.exceptions().isEmpty()) { - logger.error("Some errors occurred whilst posting event {}.", event); - int i = 0; - for (Throwable exception : result.exceptions().values()) { - logger.error("#{}: \n", ++i, exception); - } - } + fireEvent(event); eventFuture.complete(event); }); return eventFuture; } + private void fireEvent(Object event) { + PostResult result = bus.post(event); + if (!result.exceptions().isEmpty()) { + logger.error("Some errors occurred whilst posting event {}.", event); + int i = 0; + for (Throwable exception : result.exceptions().values()) { + logger.error("#{}: \n", ++i, exception); + } + } + } + private void unregisterHandler(EventHandler handler) { bus.unregister(s -> s instanceof KyoriToVelocityHandler && ((KyoriToVelocityHandler) s).handler == handler); @@ -169,6 +174,11 @@ public class VelocityEventManager implements EventManager { return service.awaitTermination(10, TimeUnit.SECONDS); } + public void fireShutdownEvent() { + // We shut down the proxy already, so the fact this executes in the main thread is irrelevant. + fireEvent(new ProxyShutdownEvent()); + } + private static class VelocityMethodScanner implements MethodScanner { @Override