If a process created in java creates a sub-process, but then returns, the JVM hangs, but with no process id.
Example application below (requires Windows and Java 7)
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.Files;
public class SubProcessHang {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "start", "notepad.exe");
File output = Files.createTempFile("output", "txt").toFile();
builder.redirectError(Redirect.to(output));
builder.redirectOutput(Redirect.to(output));
Process process = builder.start();
process.waitFor();
int exitValue = process.exitValue();
System.out.println("Process exit value:: " + exitValue);
System.out.println("Output file length:: " + output.length());
System.exit(exitValue);
}
}
When the application runs, it creates three processes: java --> cmd --> notepad cmd immediately returns and java calls System.exit(0), which kills the java process. But notepad is still there, and, when run from gradle (or eclipse for that matter,) the JVM hangs around until that process goes away, not returning it's return value.
So the children process is still alive but the parent process has been partially killed, but is now stranded forever.
The build.gradle script to reproduce this
apply plugin: 'java'
apply plugin: 'application'
mainClassName = "SubProcessHang"
Execute 'gradle run' and get this output:
C:\HangDemo>gradlew run
:compileJava
:processResources UP-TO-DATE
:classes
:run
Process exit value:: 0
Output file length:: 0
> Building 75% > :run
I know it must have something to do with how the java processes are created, but I have no idea what to do.
What can I do short of getting the ID of the running java process and killing all sub-processes in a shutdown hook?