United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6563368 : com.sun.net.httpserver not closing connections

Details
Type:
Bug
Submit Date:
2007-05-30
Status:
Closed
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2007-07-31
Component:
core-libs
OS:
linux
Sub-Component:
java.net
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

Description
FULL PRODUCT VERSION :
Sun JDK 1.6.0 [sun-jdk-1.6]


ADDITIONAL OS VERSION INFORMATION :
Linux camelion 2.6.20-gentoo #1 PREEMPT Sun Feb 11 23:37:40 CET 2007 i686 Intel(R)
ntium(R) 4 Mobile CPU 1.80GHz GenuineIntel GNU/Linux

A DESCRIPTION OF THE PROBLEM :
The httpServer is not closing the connection after the request is completed.
the number of open connections increase until the system limit is reached.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the attached code and start using
javac WebApp.java
java WebApp

open a second terminal
while true; do curl http://localhost:8123/test; done

open a third terminal
netstat -atunp | grep 8123 | wc -l


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The connections should be closed after the request is processed.
ACTUAL -
Connection keeps open.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class WebApp implements HttpHandler {

    public static void main(String[] args) {
        try {
            HttpServer server = HttpServer.create(new InetSocketAddress(8123),0);
            HttpContext ctx = server.createContext("/test",new WebApp());
            server.setExecutor(null);
            server.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void handle(HttpExchange t) throws IOException {
        InputStream is = t.getRequestBody();
        byte[] bBody = new byte[1000];
        int count = is.read(bBody);
        String sBody = bBody.toString();
        String response = "testmessage\n";
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        is.close();
        os.close();
        t.close();
    }
}
---------- END SOURCE ----------

                                    

Comments
EVALUATION

If you look at the state of these sockets you will see that they are all in the TIME_WAIT
state, which means they have been closed. TCP port numbers (and therefore the socket) cannot
be reused until the TIME_WAIT period has elapsed.

The number of idle connections which the server keeps open and the maximum idle period for
a connection are controlled by the following system properties (respectively)

sun.net.httpserver.maxIdleConnections
sun.net.httpserver.idleInterval

So, this not a bug.
                                     
2007-07-31



Hardware and Software, Engineered to Work Together