Running subprocess on Windows with inheritIO never completes


I’m building a plugin which uses the Java ProcessBuilder to launch a subprocess. inheritIO() is then used to pipe subprocess stdout to Gradle’s stdout. However, nothing gets printed on Gradle stdout; what’s even worse is that the subprocess appears to freeze and will never finish, blocking Gradle from finishing as well.

The issue is only reproducible on Windows 10, most probably because Windows 10 stdout buffer is rather small. I assume that when the stdout buffer overflows, the child process is stopped until the stdout buffer is emptied. However, I thought that when using inheritIO(), the child stdout is simply dumped to the parent stdout automatically, and there is no need to ‘pump’ bytes from the child stdout manually.

This issue is somewhat similar to How to redirect the output of a ProcessBuilder to the gradle console? however in this topic I’d like to discuss the freezing aspect. The related topic suggests to use DefaultExecAction instead. Unfortunately that is not applicable to my use case since the ProcessBuilder-related code resides in a jar which is common both for Maven and Gradle plugins.

Is there a way to force Gradle to pipe the output of child’s stdout to Gradle’s stdout and still use inheritIO() please? If not, could you please suggest a workaround that doesn’t depend on Gradle internals?

The issue is very easy to reproduce: just create the build.gradle.kts file with the following contents:

tasks.register("hello") {
    doLast {
        ProcessBuilder("cmd", "/c", "type", "foo.txt").inheritIO().start().waitFor()

The same code will work when launched from a main() method, which implies that the problem doesn’t lie in the code itself.

The problem has been found in the Vaadin Gradle plugin; reference issue ticket is here:

Opened a bug report: Running subprocess on Windows with inheritIO never completes · Issue #16716 · gradle/gradle · GitHub

1 Like