This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH: remove all traces of java.io.FileDescriptor from java.net


Tom Tromey wrote:
> Meanwhile, I think we definitely must fix this bug for 3.1.  I
> submitted a PR so we can track the issue.

I wrote a semi-simple test case for this bug.  It's attached.

Unfortunately, it does not seem to be the cause of the problem I was 
seeing.  With the version of GCJ I'm using, this test case succeeds. 
With the cvs version of the 3.1 branch as of 20020322 at around 10AM, it 
fails most of the time.  Strangely, when running about 100 times, I saw 
it succeed 3 times.

Tomorrow, I'm going to try digging into things and try to figure out 
what the differences are between my older version and the current cvs 
3.1 branch head to see why I'm succeeding and the current is not.

regards,
michael


[msmith@msmith SocketClose]$ /usr/local/bin/gcj -v
{snip}
gcc version 3.1 20020322 (prerelease)
[msmith@msmith SocketClose]$ export 
LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/gcc-lib/
[msmith@msmith SocketClose]$ /usr/local/bin/gcj --main=FDGone -o FDGone 
FDGone.java
[msmith@msmith SocketClose]$ ./FDGone
Failed! Exception: java.io.IOException: Bad file descriptor
[msmith@msmith SocketClose]$ /home/msmith/GCC-3.1/bin/gcj -v
{snip}
gcc version 3.1 20010911 (experimental)
[msmith@msmith SocketClose]$ export 
LD_LIBRARY_PATH=/home/msmith/GCC-3.1/lib/:/home/msmith/GCC-3.1/lib/gcc-lib/i686-pc-linux-gnu/3.1/
[msmith@msmith SocketClose]$ /home/msmith/GCC-3.1/bin/gcj --main=FDGone 
-o FDGone FDGone.java
[msmith@msmith SocketClose]$ ./FDGone
Success.

grr...
import java.io.*;
import java.net.*;

public class FDGone {
    private static int port = 54982;

    /**
     *  Flag indicating whether the test is being run "manually" on
     *  the command line.  When true, this adds some additional
     *  printing, and some pauses so that the person executing the
     *  test can check the file descriptors for the process using
     *  something like "ls -l /proc/<pid>/fd"
     **/
    private static boolean manual = false;

    public static class EchoServer extends Thread {
	private ServerSocket ss;
	public EchoServer() throws IOException {
	    ss = new ServerSocket(port);
	}

	public void run() {
	    while(true) {
		Socket s = null;
		try {
		    s = ss.accept();
		    
		    byte[] buf = new byte[1024];
		    InputStream in = s.getInputStream();
		    OutputStream out = s.getOutputStream();
		    int count;
		    while((count = in.read(buf, 0, buf.length)) != -1) {
			if(count == 0) continue;
			out.write(buf, 0, count);
		    }
		} catch (Exception e) {
		    System.err.println("Exception echoing stream: " + e);
		} finally {
		    try {
			if(s != null) s.close();
		    } catch (Exception e) {
		    }
		}
	    }
	}
    }

    private static final File FILE;
    static {
	File f = null;
	try {
	    f = File.createTempFile("bug6053-", null);
	    f.deleteOnExit();
	    FileOutputStream out = new FileOutputStream(f);
	    out.write("testfile".getBytes());
	    out.flush();
	    out.close();
	    out = null;
	    System.gc();
	    System.runFinalization();
	} catch (Exception e) {
	    System.err.println("Unable to run test: " + e);
	    System.exit(1);
	}
	FILE = f;
    }


    public static class Client {

	public void go() throws Exception {

	    Socket s = new Socket("localhost", port);
	    OutputStream sout = s.getOutputStream();
	    InputStream sin = s.getInputStream();
	    
	    sout.write("hello".getBytes());
	    byte[] buf = new byte[10];
	    int count = sin.read(buf);

	    msg("Socket opened.");

	    s.close();
	    
	    msg("Socket closed.");	    
	}

    }

    public static void main(String[] args) throws Exception {

	EchoServer server = new EchoServer();
	server.start();

	msg("Base state");

	Client c = new Client();
	c.go();

	FileInputStream fin = new FileInputStream(FILE);
	DataInputStream in = new DataInputStream(fin);

	msg("Opened file");
	
	c = null;
	System.gc();
	System.runFinalization();

	msg("After garbage collection");

	try {
	    in.read();

	    System.err.println("Success.");
	} catch (Exception e) {
	    System.err.println("Failed! Exception: " + e);
	    System.exit(1);
	}
	System.exit(0);
    }

    private static void msg(String message) {
	if(manual) {
	    System.err.println("Check file descriptors.  Message: " + message);
	    try { Thread.sleep(10000); } catch (Exception e) {}
	}
    }

}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]