United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6368984 configuring unconnected Socket before passing to implAccept can cause fd leak
JDK-6368984 : configuring unconnected Socket before passing to implAccept can cause fd leak

Details
Type:
Bug
Submit Date:
2006-01-05
Status:
Closed
Updated Date:
2011-03-07
Project Name:
JDK
Resolved Date:
2011-03-07
Component:
core-libs
OS:
generic
Sub-Component:
java.net
CPU:
generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.0
Fixed Versions:

Related Reports
Backport:

Sub Tasks

Description
See the following thread on the RMI-USERS mailing list:
	http://archives.java.sun.com/cgi-bin/wa?A2=ind0601&L=rmi-users&P=265

It seems that certain uses of an unconnected Socket before passing it to ServerSocket.implAccept can cause a file descriptor leak.  While the fix seems to be to reorder the impl-creating operations to after the implAccept invocation, it seems like a possible bug (or at least a documentation issue) that this leak can happen silently (until all file descriptors are exhausted).  (Also, the case with setSoTimeout worked with 1.3, for what that's worth.)  Compare the behavior of the following example between 1.3 and later:

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

public class AcceptDescriptorLeak {
    private static final int REPS = 10000;
    public static void main(String[] args) throws Exception {
	final ServerSocket ss = new ServerSocket(0) {
	    public Socket accept() throws IOException {
		Socket s = new Socket() { };
		s.setSoTimeout(10000);
		implAccept(s);
		return s;
	    }
	};
	Thread t = new Thread(new Runnable() {
	    public void run() {
		try {
		    for (int i = 0; i < REPS; i++) {
			(new Socket("localhost", ss.getLocalPort())).close();
		    }
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	});
	t.setDaemon(true);
	t.start();
	for (int i = 0; i < REPS; i++) {
	    ss.accept().close();
	}
	ss.close();
    }
}

                                    

Comments
EVALUATION

There's a fd leak, indeed. Will fix.
                                     
2006-08-30



Hardware and Software, Engineered to Work Together