prezi / gradle-haxe-plugin

Gradle Haxe Plugin
Other
23 stars 4 forks source link

Added new thread to read from subprocess's stdout #29

Closed exFalso closed 9 years ago

exFalso commented 9 years ago

Fixes https://github.com/prezi/gradle-haxe-plugin/issues/28

exFalso commented 9 years ago

If you read the stdout from the same thread that you forked the subprocess from, then your waitFor() call will hang, as nobody's reading the subprocess's stdout until it finishes, meaning a buffer will get full and the processes will deadlock.

The solution is to spawn a new thread that reads the subprocess's stdout while the original thread is blocking on waitFor().

References: http://stackoverflow.com/questions/3807834/processbuilder-gets-stuck-after-getting-an-error http://steveliles.github.io/invoking_processes_from_java.html

lptr commented 9 years ago

If you invoke waitFor() after reading the contents, it's okay. Have you tried this?

        Process process = builder.start();

        // remove it from here
        // process.waitFor();

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ByteStreams.copy(process.getInputStream(), bytes);
        String output = bytes.toString(Charsets.UTF_8.name());

        // move it to here
        process.waitFor();
exFalso commented 9 years ago

makes sense, https://github.com/prezi/gradle-haxe-plugin/commit/ee30f8602887788b06cac3da9387a062c74a92b3