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

PipedInputStream / PipedOutputStream: Allow manually breaking pipe


    • Subcomponent:
    • CPU:
    • OS:


      In the following I will refer to the reading and writing thread as reading / writing side.
      Currently if one side encounters a problem, e.g. the data source used by the writing side
      threw an exception or the reading side failed consuming the data, there is no clear way to
      indicate this failure to the other side.

      The other side might be currently blocked, because it is waiting for data to read, respectively
      waiting for free space in the buffer when trying to write. There is no way to notify (unblock) it.

          PipedOutputStream out = new PipedOutputStream();
          PipedInputStream in = new PipedInputStream(out);
          new Thread(() -> {
              try {
              catch (SomeDataProviderException e) {
                  // Have to notify the reading side, but how?
          try {
          catch (SomeDataConsumerException e) {
              // Have to notify the writing side, but how?


      ENHANCEMENT REQUEST: It would be useful if both sides could manually break the pipe.


          try {
          catch (SomeDataProviderException e) {
              // Break the pipe to prevent the reading side from infinitely waiting for data


      Below are some implementation restrictions I came up with in case you are interested:
          - Both sides should be able to break the pipe:
              - e.g. when the writer failed getting data to write
              - e.g. when the reader failed consuming the data
          - The pipe can only be broken on one side if:
              - It has not been broken on the same side before
              - The pipe has not been closed on the same side
          - If one side broke the pipe, the other should be able to break it as well. Rationale:
              - The intention for breaking the pipe is to inform the other side. If the other side
                encountered a problem and wants to break the pipe as well, there is no need to inform
                it that the pipe is already broken.
              - After one side encountered a problem and broke the pipe it is expected to handle the
                problem (e.g. by logging), so in case both sides encountered a problem, both will
                handle it and no information is lost.
          - The writing side should fail when trying to write to a broken pipe
          - For consistency with the current behavior the reading side should only detect that the pipe
            has been manually broken by the writing side once no data is left in the buffer
          - If one side manually broke the pipe, the other should not check whether the thread on that
            side is still alive(=current detection of a broken pipe)
          - A pipe should be manually breakable by providing an exception:
              - breakPipe(Throwable cause)
              - The other side should not directly throw that exception, but wrap it into a
                new exception to create a proper stack trace (see also JDK-6350053 for a possible exception




            • Assignee:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              2 Start watching this issue


              • Created: