RFR: 8364361: [process] java.lang.Process should implement Closeable [v16]
Roger Riggs
rriggs at openjdk.org
Fri Oct 17 01:32:55 UTC 2025
> 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 with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 24 additional commits since the last revision:
- Merge branch 'master' into 8364361-process-autocloseable
- Modify close to waitFor process to exit.
If the waitFor is interrupted, destroy the process (forcibly).
Wait indefinately for the process to terminate.
Updated tests with exit status indicating waiting for termination
due to reduction in racy exits.
- The ProcessCloseTest is modified to be able to test the logic
around close() waiting for the process to exit and the specified
behavior of interrupting the waitFor.
- Close is modified to wait for the process to terminate before returning.
Please review the revised javadoc.
As suggested in review comments, waiting for the process to terminate
allows a more orderly cleanup by the application.
The streams are closed and close uses `waitFor()` with an unlimited timeout
for the process to terminate.
While waiting the thread can be `interrupted` to exit the `waitFor`.
If/when `waitFor` is interrupted, the process is destroyedForcibly on
all platforms. When close() returns, the thread interrupt will pending
and can be handled by the caller.
If a watchdog timeout is desired on close(), a separate thread
can schedule an interrupt at a suitable time after close is called.
- Review comment improvements to make expected behavior clearer.
- Merge branch 'master' into 8364361-process-autocloseable
- Clarify the use of waitFor after close() or T-W-R exit.
- Additional review feedback.
Updated to emphasize reading the data from the streams and calling waitFor
to allow for normal termination.
- Merge branch 'master' into 8364361-process-autocloseable
- Address review comments for code and javadoc in Process, the test and the example.
Reinforced that calling waitFor should occur before calling close to wait for termination and get the exitStatus.
Corrected the error message check for invalid handles on Windows.
Update test to allow normal completion when exceptions may be expected.
Correct the expected message on Windows for an invalid handle exception.
- ... and 14 more: https://git.openjdk.org/jdk/compare/968f39cf...daefd795
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/26649/files
- new: https://git.openjdk.org/jdk/pull/26649/files/6f8f7327..daefd795
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=26649&range=15
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=26649&range=14-15
Stats: 25187 lines in 538 files changed: 16551 ins; 6932 del; 1704 mod
Patch: https://git.openjdk.org/jdk/pull/26649.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/26649/head:pull/26649
PR: https://git.openjdk.org/jdk/pull/26649
More information about the core-libs-dev
mailing list