United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6219755 PipedOutputStream.write() remains blocked after PipedInputStream was closed
JDK-6219755 : PipedOutputStream.write() remains blocked after PipedInputStream was closed

Details
Type:
Bug
Submit Date:
2005-01-20
Status:
Closed
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2005-10-14
Component:
core-libs
OS:
generic
Sub-Component:
java.io
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Backport:
Backport:

Sub Tasks

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.
                                     
2005-09-08



Hardware and Software, Engineered to Work Together