On JDK 23 on Windows a child that nobody read from was blocked by 4,608 bytes of output. Drain both streams, merge them, redirect to a file or discard, and bound the wait.
A job that hangs on some inputs
Dinky maintains a nightly Java job at Belgavi's Book Store that converts supplier catalogue files by running an external command-line tool. For months it worked. Then a supplier sent a larger file, the tool printed more warnings than usual, and the job stopped. No exception, no CPU use, no log line after "starting conversion". A thread dump showed the main thread inside Process.waitFor(). The child process was alive too, also doing nothing.
The cause is one of the oldest traps in process handling. The parent was waiting for the child to exit before reading its output. The child was waiting for someone to read its output before it could write more. Neither would move first.
This article measures where that happens on JDK 23, explains why the threshold differs between operating systems, and shows the four reliable ways to handle a child's output: read it concurrently, merge the streams, redirect to a file, or discard it.
How the output of a child reaches the parent
When Java starts a process with ProcessBuilder and the default settings, the child's standard output and standard error are each connected to an operating-system pipe. The parent reads them through Process.getInputStream() and Process.getErrorStream(). A pipe has a fixed buffer in the kernel. While the buffer has room, the child's writes return immediately. When the buffer is full, the child's next write blocks until the parent reads.
The java.lang.Process documentation says this directly: "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 process may cause the process to block, or even deadlock."
How limited depends on the platform. On Linux the pipe(7) manual page gives a default capacity of 16 pages since kernel 2.6.11, which is 65,536 bytes with 4,096-byte pages. On Windows, the JDK creates the pipes itself; the JDK 23 source for the Windows process implementation defines PIPE_SIZE as 4096 + 24 bytes.
Measuring the threshold
The companion is a single Java file that starts itself as the child. The child writes a chosen number of bytes to standard output in 512-byte chunks, flushing each one, and exits. The parent uses the anti-pattern deliberately: it calls waitFor with a 4-second timeout before reading anything, and records whether the child exited. A binary search finds the boundary.
On JDK 23.0.2 on Windows 11 the recorded result is:
| Output size | Wait first, read later |
|---|---|
| 1,000 bytes | child exits |
| 4,096 bytes | child exits |
| 4,608 bytes | child still running after 4 seconds |
| 1,000,000 bytes | child still running after 4 seconds |
A child that prints a few kilobytes works every time in testing. The same code hangs for good once the output grows past one buffer, which is exactly what happened with the larger supplier file. On Linux the boundary would sit near 64 KiB instead, so a job developed on Linux and deployed on Windows can start hanging with no code change. The companion records only the Windows run; its run.sh compares against a Linux recording only if one has been added.
The second trap: reading one stream at a time
Many developers know to read the output before calling waitFor. A common next mistake is to read standard output to the end and then read standard error.
The companion's child writes all of its standard output first and then all of its standard error. The parent reads standard output in full, then standard error. With 1,000 bytes on each stream it works. With 1,000,000 bytes on each, the parent finishes reading standard output, but standard output never reaches end of stream, because the child has not exited: it is blocked writing to the full standard-error pipe that nobody is reading. The recorded line reads "stdout never reached end of stream within 4000 ms".
The rule that follows: while a child is running, both of its output streams must be drained, or redirected somewhere that does not need draining.
Four ways that work
The companion runs each of these with 1,000 and 1,000,000 bytes on both streams. All of them exit with status 0 and lose nothing.
Drain both streams concurrently. Read standard error on a second thread while the current thread reads standard output.
Process p = new ProcessBuilder(cmd).start();
CompletableFuture<byte[]> err = CompletableFuture.supplyAsync(() -> readAll(p.getErrorStream()));
byte[] out = p.getInputStream().readAllBytes();
if (!p.waitFor(30, TimeUnit.SECONDS)) {
p.destroyForcibly();
throw new IllegalStateException("converter timed out");
}
byte[] errBytes = err.join();Use this when you need the two streams separately, for example to parse standard output as data and log standard error. supplyAsync without an executor runs on the common pool; in a server, pass a dedicated executor so blocking reads do not occupy shared threads.
Merge standard error into standard output. redirectErrorStream(true) sends both streams into one pipe, so a single read loop drains everything. The companion read 2,000,000 bytes for a child that wrote 1,000,000 to each stream. Use this when the output is a log for people, not data to parse.
Redirect to a file. redirectOutput(file) and redirectError(file), or redirectErrorStream(true) with one file, let the operating system write directly to disk. The parent never touches a pipe, so nothing can fill. This is the most robust choice for long-running tools with large or unpredictable output, and it leaves a record you can attach to an incident.
Discard. ProcessBuilder.Redirect.DISCARD, available since Java 9, sends output to the null device. The child cannot block on it. Use it only when you are sure the output carries nothing you will need, and keep the exit code check.
Always bound the wait
Even with the streams handled, a child can hang for its own reasons. waitFor(timeout, unit) returns false if the process is still running when the time is up; then call destroy() or destroyForcibly() and decide whether the job fails or retries. On a deadline, consider what the tool may have done partially: a converter killed half-way through writing its output file has left a half-written file, so write to a temporary path and rename when the process succeeds.
Standard input is a pipe too
The same reasoning applies in the other direction. If the parent writes a large input to the child's standard input while the child writes a large output that nobody reads, both can block: the child stops reading input because its output is full, and the parent stops writing because the input pipe is full. Write input on its own thread, or redirect input from a file with redirectInput(file), and close the child's standard input when you are done so the child sees end of file.
Finding the bug in existing code
The pattern is easy to search for:
waitFor()before any read ofgetInputStream()orgetErrorStream();- reading only one of the two streams when standard error is not merged or redirected;
readAllBytes()or a read loop on standard output followed by a read of standard error;- code that never reads the streams at all and does not redirect them.
A thread dump during a hang shows the parent in waitFor or in a stream read, and the child process alive with no CPU use. On Linux, ls -l /proc/PID/fd or lsof -p PID shows the child's output descriptors pointing at pipes.
A regression test does not need the real tool. Start a child that writes several megabytes to each stream (the companion starts itself, so only a JDK is needed), run your process wrapper with a short timeout, and assert that it completes and captures everything. A few megabytes is enough on any platform: it exceeds both the Windows and the Linux pipe buffers.
Limits
The measurements come from one machine: JDK 23.0.2 on Windows 11, with the child writing in 512-byte chunks. The exact threshold depends on the chunk size, the platform and the JDK's own buffering, so treat 4,096 bytes as an illustration of "one small buffer", not a constant to design around. The Linux figure comes from the pipe(7) manual page and was not measured here. The probe is compiled with --release 17, so it uses only Java 17 APIs.