Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8364362

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

XMLWordPrintable

    • source, behavioral
    • low
    • Hide
      The compatibility risk is minimal for the addition of the `close()` method and implementation of `Closeable` and `AutoCloseable`.
      java.lang.Process is not final and subclasses exist in OpenJDK itself and in other libraries. Clients of an existing subclass that implements a `close()` method will not be affected since the overridden implementation is used, not the implementation in Process. in a corpus search a few subclasses of Process were found that implemented a `close` method. All implementations of close() destroyed the Process, either by delegating to Process or by using OS specific APIs or commands to kill the process. None of the implementations closed the streams.
      Show
      The compatibility risk is minimal for the addition of the `close()` method and implementation of `Closeable` and `AutoCloseable`. java.lang.Process is not final and subclasses exist in OpenJDK itself and in other libraries. Clients of an existing subclass that implements a `close()` method will not be affected since the overridden implementation is used, not the implementation in Process. in a corpus search a few subclasses of Process were found that implemented a `close` method. All implementations of close() destroyed the Process, either by delegating to Process or by using OS specific APIs or commands to kill the process. None of the implementations closed the streams.
    • Java API
    • SE

      Summary

      With the introduction of Process.close(), process resource cleanup is now straightforward and complete. By launching a process inside a try-with-resources block, both the process streams and the process itself are properly cleaned up and terminated when exiting the block.

      Problem

      Previously, callers were responsible for manually closing each process stream and ensuring the process itself was terminated, as there was no unified method to handle all cleanup. While try-with-resources could manage streams, it could not be applied to Process directly since it lacked a close() method and did not implement AutoCloseable or Closeable.

      Solution

      The addition of Process.close() and the implementation of Closeable and AutoCloseable interfaces to java.lang.Process allow for consistent and reliable cleanup. Now, try-with-resources can be used with Process to automatically close its streams and terminate the process upon exit from try-with-resources. Inside a try-with-resources block, applications have full access to process APIs and streams, enabling them to read, write, close streams, wait for process completion, and check exit status as needed.

      Specification

      /**
       * Close all writer and reader streams and terminate the process.
       * The streams are closed immediately and the process is terminated without waiting.
       * This method is idempotent, if the process has already been closed
       * invoking this method has no effect.
       * <p>
       * Before calling {@code close} the caller should read the streams for any
       * data or text and call {@linkplain #waitFor() waitFor} if the exit value is needed.
       * The contents of streams that have not been read fully are lost;
       * they are discarded or ignored.
       * Streams should be {@code closed} when no longer needed.
       * Closing an already closed stream usually has no effect but is specific to the stream.
       * If an {@code IOException} occurs when closing a stream it is
       * re-thrown after the process is destroyed. Additional {@code IOExceptions}
       * thrown by closing the remaining streams, if any, are added to the first
       * {@code IOException} as {@linkplain IOException#addSuppressed suppressed exceptions}.
       * <p>
       * The process may already have exited or be in the process of exiting;
       * if it is {@linkplain #isAlive() alive}, it is {@linkplain #destroy destroyed}.
       * <p>
       * Example using try-with-resources writing text to a process, reading back the
       * response, and closing the streams and process:
       * {@snippet class=ProcessExamples region=example}
       *
       * @implSpec
       * The {@code outputWriter} and {@code outputStream} to the process are closed.
       * The {@code inputReader} and {@code inputStream} from the process are closed.
       * The {@code errorReader} and {@code errorStream} from the process are closed.
       * The process is destroyed.
       * @throws IOException if closing any of the streams throws an exception
       * @since 26
       */
      public void close() {...}

      The apidiff and javadoc are attached.

            rriggs Roger Riggs
            rriggs Roger Riggs
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: