RFR: 8364361: [process] java.lang.Process should implement close and be AutoCloseable

Roger Riggs rriggs at openjdk.org
Wed Aug 6 15:44:05 UTC 2025


On Wed, 6 Aug 2025 08:09:56 GMT, Volkan Yazici <vyazici at openjdk.org> 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.
>
> src/java.base/share/classes/java/lang/Process.java line 668:
> 
>> 666:         // Wait briefly for process to exit, if not exited immediately, destroy
>> 667:         try {
>> 668:             boolean alive = waitFor(Duration.ofMillis(2000));
> 
> Doesn't this go against the `the process is terminated without waiting` statement in the spec?

The spec and the implementation were from different trains of thought at different times and are inconsistent.
The spec says no waiting; the implementation of the waitFor mixes in both some delay and the complexity associated with interrupts. The delay may not be long enough for a particular platform or application to achieve its attempt at being nice and is fragile. The fragility introduced is not worth the minimal benefit.

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

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


More information about the core-libs-dev mailing list