Skip to content

Commit 310be23

Browse files
committed
Registered shutdown hook to kill JVM forked by spring-boot:run
Previously, a JVM that was forked by spring-boot:run could be orphaned when the parent process (the Maven build) was terminated in an IDE. Note that this doesn’t happen when spring-boot:run is invoked from a shell. This commits add a shutdown hook that registered when RunMojo forks the JVM. The shutdown hook attempts to kill the forked JVM’s RunProcess rather than relying on the death of the parent process being sufficient to also kill the child. Closes gh-5815
1 parent 9a8e469 commit 310be23

File tree

1 file changed

+21
-3
lines changed
  • spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven

1 file changed

+21
-3
lines changed

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
*
3434
* @author Phillip Webb
3535
* @author Stephane Nicoll
36+
* @author Andy Wilkinson
3637
*/
3738
@Mojo(name = "run", requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE, requiresDependencyResolution = ResolutionScope.TEST)
3839
@Execute(phase = LifecyclePhase.TEST_COMPILE)
@@ -41,8 +42,10 @@ public class RunMojo extends AbstractRunMojo {
4142
@Override
4243
protected void runWithForkedJvm(List<String> args) throws MojoExecutionException {
4344
try {
44-
new RunProcess(new JavaExecutable().toString()).run(true,
45-
args.toArray(new String[args.size()]));
45+
RunProcess runProcess = new RunProcess(new JavaExecutable().toString());
46+
Runtime.getRuntime()
47+
.addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
48+
runProcess.run(true, args.toArray(new String[args.size()]));
4649
}
4750
catch (Exception ex) {
4851
throw new MojoExecutionException("Could not exec java", ex);
@@ -82,4 +85,19 @@ private void join(ThreadGroup threadGroup) {
8285
while (hasNonDaemonThreads);
8386
}
8487

88+
private static class RunProcessKiller implements Runnable {
89+
90+
private final RunProcess runProcess;
91+
92+
private RunProcessKiller(RunProcess runProcess) {
93+
this.runProcess = runProcess;
94+
}
95+
96+
@Override
97+
public void run() {
98+
this.runProcess.kill();
99+
}
100+
101+
}
102+
85103
}

0 commit comments

Comments
 (0)