RFR: 8364361: [process] java.lang.Process should implement Closeable [v15]
Roger Riggs
rriggs at openjdk.org
Wed Oct 8 02:11:53 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 22 additional commits since the last revision:
- 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.
- Update close() to use "terminate" consistently.
- Remove volatile from "closed" field; updates are guarded by synchronized.
- ... and 12 more: https://git.openjdk.org/jdk/compare/73753a41...6f8f7327
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/26649/files
- new: https://git.openjdk.org/jdk/pull/26649/files/18637975..6f8f7327
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=26649&range=14
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=26649&range=13-14
Stats: 10946 lines in 514 files changed: 5350 ins; 2684 del; 2912 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