14

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?

2
  • This doesn't look like it is really anything to do with gradle to me. Have you tried just running your class from a command line? Commented Feb 21, 2014 at 0:29
  • 1
    Yes. It works as expected (as I would expect, anyway) from command line. It returns the exit status, and notepad is still running. The problem is gradle is the automation tool that is running the java process modeled here, and never returns.
    – w25r
    Commented Feb 21, 2014 at 21:47

2 Answers 2

3

The docs for Process say

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Maybe your process is creating stdout or stderr output. Try draining the InputStream and the ErrorStream.

3
  • 1
    I am piping the output streams of the actual offending process. That is not the problem. In the example, notepad.exe has no output. It's simply that it is still alive.
    – w25r
    Commented Feb 21, 2014 at 21:45
  • What about the streams of the process you exec'ed? In your example, I'm talking about draining p.getInputStream() and p.getErrorStream() Commented Feb 24, 2014 at 3:33
  • I've clarified the example to show that's not the issue. When that's a problem, normally process.waitFor() is what hangs, and the JVM threads stay alive. Neither of those is happening here.
    – w25r
    Commented Feb 25, 2014 at 16:40
2

I would say that this answer might help with getting the sub-process IDs and this one - with killing them in Windows environment.

Hope that helps!

1
  • So the problem with this is that the java process starts the cmd process which starts the notepad process... but then the cmd process goes away. Which is the only link between java and notepad. So you can't even kill all children processes because it doesn't know about notepad.exe anymore.
    – w25r
    Commented Mar 4, 2014 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.