From f49f77e9f60b5e977c69e33de86847b94e2589db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=B4=E8=8A=82?= Date: Tue, 16 Jan 2024 11:09:28 +0800 Subject: [PATCH] avoid npe in SpringContextInstallStage when interrupted --- .../isle/stage/SpringContextInstallStage.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/boot/isle/stage/SpringContextInstallStage.java b/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/boot/isle/stage/SpringContextInstallStage.java index 85688f383..97942557f 100644 --- a/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/boot/isle/stage/SpringContextInstallStage.java +++ b/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/boot/isle/stage/SpringContextInstallStage.java @@ -186,34 +186,37 @@ private void doRefreshSpringContextParallel() { */ private void refreshRecursively(DeploymentDescriptor deployment, CountDownLatch latch, List> futures) { - futures.add(moduleRefreshExecutorService.submit(() -> { - String oldName = Thread.currentThread().getName(); - try { - Thread.currentThread().setName( - "sofa-module-refresh-" + deployment.getModuleName()); - if (deployment.isSpringPowered() && !application.getFailed().contains(deployment)) { - refreshAndCollectCost(deployment); - } - DependencyTree.Entry entry = application - .getDeployRegistry().getEntry(deployment.getModuleName()); - if (entry != null && entry.getDependsOnMe() != null) { - for (DependencyTree.Entry child : entry - .getDependsOnMe()) { - child.getDependencies().remove(entry); - if (child.getDependencies().size() == 0) { - refreshRecursively(child.get(), latch, futures); + // if interrupted, moduleRefreshExecutorService will be null; + if (moduleRefreshExecutorService != null) { + futures.add(moduleRefreshExecutorService.submit(() -> { + String oldName = Thread.currentThread().getName(); + try { + Thread.currentThread().setName( + "sofa-module-refresh-" + deployment.getModuleName()); + if (deployment.isSpringPowered() && !application.getFailed().contains(deployment)) { + refreshAndCollectCost(deployment); + } + DependencyTree.Entry entry = application + .getDeployRegistry().getEntry(deployment.getModuleName()); + if (entry != null && entry.getDependsOnMe() != null) { + for (DependencyTree.Entry child : entry + .getDependsOnMe()) { + child.getDependencies().remove(entry); + if (child.getDependencies().size() == 0) { + refreshRecursively(child.get(), latch, futures); + } } } + } catch (Throwable t) { + LOGGER.error(ErrorCode.convert("01-11002", deployment.getName()), t); + throw new RuntimeException(ErrorCode.convert("01-11002", deployment.getName()), + t); + } finally { + latch.countDown(); + Thread.currentThread().setName(oldName); } - } catch (Throwable t) { - LOGGER.error(ErrorCode.convert("01-11002", deployment.getName()), t); - throw new RuntimeException(ErrorCode.convert("01-11002", deployment.getName()), - t); - } finally { - latch.countDown(); - Thread.currentThread().setName(oldName); - } - })); + })); + } } protected void refreshAndCollectCost(DeploymentDescriptor deployment) {