On Mon, 22 Sep 2025 22:25:59 GMT, Roger Riggs <[email protected]> wrote:

>> The teardown of a Process launched by `ProcessBuilder` includes the closing 
>> of streams and ensuring the termination of the process is the responsibility 
>> of the caller. The `Process.close()` method provides a clear and obvious way 
>> to ensure all the streams are closed and the process terminated.
>> 
>> The try-with-resources statement is frequently used to open streams and 
>> ensure they are closed on exiting the block. By implementing 
>> `AutoClosable.close()` the completeness of closing the streams and process 
>> termination can be done by try-with-resources.
>> 
>> The actions of the `close()` method are to close each stream and destroy the 
>> process if it has not terminated.
>
> Roger Riggs has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   Update close() to use "terminate" consistently.

src/java.base/share/classes/java/lang/Process.java line 186:

> 184:      * <p>
> 185:      * Before calling this method the caller should read the streams for 
> any
> 186:      * data or text and call {@linkplain #waitFor() waitFor} if the exit 
> value is needed.

Should we add some additional text which clearly highlights that waitFor() 
needs to be called (before close()) to allow the process to run to completion, 
even when the exit value or the stream contents aren't used/read by the calling 
application? I realize that a few lines above we specify:

> The streams are closed immediately and the process is terminated without 
> waiting.

But maybe explicitly saying that waitFor() is necessary for allowing the 
process to run to completion would help reduce confusion? When I first saw the 
PR, my initial thought was `Process.close()` would wait for the process to run 
to completion and cleanup the resources that were used and I suspect it might 
become a common pattern where applications do something like:


try (Process p = new ProcessBuilder("some process that takes 5 seconds to 
complete normally").start()) {
}

and be surprised that the launched process was abruptly terminated. So maybe 
some clear text on this method would help?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/26649#discussion_r2385808375

Reply via email to