From 73662f9ef06880db16714e6604bb0d73132e8469 Mon Sep 17 00:00:00 2001 From: HzjNeverStop <441627022@qq.com> Date: Tue, 16 Jan 2024 14:01:38 +0800 Subject: [PATCH] Avoid npe in SpringContextInstallStage when interrupted (#1280) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit avoid npe in SpringContextInstallStage when interrupted --------- Co-authored-by: 致节 --- .../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) {