fix: ShutdownHook 改为同步等待 vertx.close(),修复 JVM 提前退出导致资源未释放

审查发现 vertx.close() 是异步操作,ShutdownHook 线程提交关闭任务后立即退出,
JVM 在资源实际释放前就终止了,与未修复时行为等价。
改为 CompletableFuture.get(10s) 阻塞等待,超时有 warn 日志。
同时移除无用的 mainVertx 字段,修正 JsExecUtils 误导性注释。
This commit is contained in:
yukaidi
2026-05-28 23:58:52 +08:00
parent 3dd4dd139b
commit 21e8a370c3
2 changed files with 7 additions and 5 deletions

View File

@@ -43,7 +43,6 @@ public final class Deploy {
private Handler<JsonObject> handle;
private Thread mainThread;
private Vertx mainVertx;
public static Deploy instance() {
return INSTANCE;
@@ -138,13 +137,16 @@ public final class Deploy {
vertxOptions.getWorkerPoolSize());
var vertx = Vertx.vertx(vertxOptions);
VertxHolder.init(vertx);
this.mainVertx = vertx;
// 注册 ShutdownHook确保进程退出时优雅关闭资源
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("JVM shutting down, closing Vert.x...");
vertx.close().onComplete(ar ->
LOGGER.info("Vert.x closed: {}", ar.succeeded() ? "success" : ar.cause().getMessage()));
try {
vertx.close().toCompletionStage().toCompletableFuture().get(10, java.util.concurrent.TimeUnit.SECONDS);
LOGGER.info("Vert.x closed successfully");
} catch (Exception e) {
LOGGER.warn("Vert.x close error or timeout", e);
}
}));
//配置保存在共享数据中
var sharedData = vertx.sharedData();

View File

@@ -62,7 +62,7 @@ public class JsExecUtils {
/**
* 调用执行js文件复用已缓存的引擎实例,避免每次创建
* 调用执行js文件使用缓存的 ScriptEngineManager 创建新引擎实例
*/
public static Object executeOtherJs(String jsText, String funName, Object ... args) throws ScriptException,
NoSuchMethodException {