JDK-6219755 : PipedOutputStream.write() remains blocked after PipedInputStream was closed
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-01-20
  • Updated: 2010-04-02
  • Resolved: 2005-10-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other Other JDK 6
5.0u18-revFixed 5.0u19Fixed 6 b57Fixed
Description
The following program works fine in 1.3 and 1.4 but does not terminate in Tiger/Mustang:

===
import java.io.*;
 
public class PipeTest {
  public static void main(String args[]) throws Exception {
    Thread t = null;
    try {
      PipedInputStream in = new PipedInputStream();
      final OutputStream out = new PipedOutputStream(in);
 
      t = new Thread("WriterThread") {
        public void run() {
          try {
            System.out.println("Writer started.");
            out.write(new byte[64*1024]);
          } catch( Throwable e ) {
            System.out.println("Writer exception:");
            e.printStackTrace();
          } finally {
            System.out.println("Writer done.");
          }
        }
      };
      t.start();
      System.out.println("Reader reading...");
      in.read(new byte[2048]);
      System.out.println("Reader closing stream...");
      in.close();
      System.out.println("Reader sleeping 3 seconds...");
      Thread.sleep(3000);
    } catch( Throwable e ) {
      System.out.println("Reader exception:");
      e.printStackTrace();
    } finally {
      System.out.println("Reader done.");
      System.out.println("Active threads:");
      Thread[] threads = new Thread[Thread.activeCount()];
      Thread.enumerate(threads);
      for( int i=0; i<threads.length; i++ ) {
        System.out.println("  " + threads[i]);
      }
      System.out.println("Waiting for writer...");
      t.join();
      System.out.println("Done.");
    }
  }
 
}
===

The expected behavior is that the call to in.close() by the reader awakes the writer (making it throw an "java.io.IOException: Pipe closed"). However, the writer remains blocked forever unless the reader Thread terminates.

The cause would seem to be in PipedInputStream.awaitSpace(), which only checks if the Thread doing the read is alive but not if the Pipe has been closed. That code was modified in Tiger by the fix for 4882082.

###@###.### 2005-1-20 21:39:52 GMT

Comments
EVALUATION This bug got accidentally introduced with the fix to:4882082 that added buffering of data to the receive(byte b[], int offset, int len) method. The fix is to enhance the checks made when a writer is waiting for the reader to make some room in the buffer for it to proceed with the further write.
08-09-2005