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

PipedOutputStream.write hangs if there is no receiver reading

XMLWordPrintable

      submitter report:
      ====


      came across a strange problem when serialising and deserialising records.
      The application seems to hang. Consider the following example.

      ---
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.PipedInputStream;
      import java.io.PipedOutputStream;
      import java.io.Serializable;
      import java.util.List;

      public class Example {

          record Moon(String name) {}

          record Planet(String name, List<Moon> moons) implements Serializable {}

          public static void main(final String[] args) throws IOException, ClassNotFoundException {
              try (PipedInputStream pipe = new PipedInputStream();
                   ObjectOutputStream out = new ObjectOutputStream(new PipedOutputStream(pipe));
                   ObjectInputStream in = new ObjectInputStream(pipe)) {
                  Planet earth = new Planet("Terra", List.of(new Moon("Luna")));
                  out.writeObject(earth);
                  out.flush();

                  Object o = in.readObject();
                  System.out.println(o);
              }
          }
      }
      ---

      The Moon record does not implement Serializable on purpose. Here I was
      expecting an exception to be thrown, but instead the program hangs. If the
      Moon record implements Serializable then the program works well.

      Am I doing something wrong?
      ====

            Unassigned Unassigned
            shadowbug Shadow Bug
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: