Skip to content

Commit e088ecd

Browse files
author
Dave Syer
committed
Re-order shutdown hook in BootRunTask
1 parent b5d65bf commit e088ecd

File tree

1 file changed

+20
-16
lines changed
  • spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run

1 file changed

+20
-16
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.gradle.process.internal.DefaultJavaExecAction;
3131
import org.gradle.process.internal.ExecHandle;
3232
import org.springframework.boot.loader.tools.FileUtils;
33-
import org.springframework.boot.loader.tools.SignalUtils;
3433
import org.springframework.util.ReflectionUtils;
3534

3635
/**
@@ -67,24 +66,29 @@ public void exec() {
6766
}
6867
}
6968

70-
private ExecResult executeReflectively() throws Exception {
69+
private ExecResult executeReflectively() throws Exception {
7170
Field builder = ReflectionUtils.findField(JavaExec.class, "javaExecHandleBuilder");
7271
builder.setAccessible(true);
7372
DefaultJavaExecAction action = (DefaultJavaExecAction) builder.get(this);
7473
setMain(getMain());
75-
final ExecHandle execHandle = action.build();
76-
ExecResult execResult = execHandle.start().waitForFinish();
77-
if (!isIgnoreExitValue()) {
78-
execResult.assertNormalExitValue();
79-
}
80-
SignalUtils.attachSignalHandler(new Runnable() {
81-
@Override
82-
public void run() {
83-
getLogger().info("Aborting java sub-process");
84-
execHandle.abort();
85-
}
86-
});
87-
return execResult;
88-
}
74+
final ExecHandle execHandle = action.build();
75+
try {
76+
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
77+
78+
@Override
79+
public void run() {
80+
getLogger().info("Aborting java sub-process");
81+
execHandle.abort();
82+
}
83+
}));
84+
} catch (Exception e) {
85+
getLogger().warn("Could not attach shutdown hook (child process may be orphaned)");
86+
}
87+
ExecResult execResult = execHandle.start().waitForFinish();
88+
if (!isIgnoreExitValue()) {
89+
execResult.assertNormalExitValue();
90+
}
91+
return execResult;
92+
}
8993

9094
}

0 commit comments

Comments
 (0)