JDK-4814794 : Too many Http connections are created by plugin
  • Type: Bug
  • Component: deploy
  • Sub-Component: plugin
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-02-07
  • Updated: 2004-05-25
  • Resolved: 2004-05-18
Related Reports
Duplicate :  
Description

Name: gm110360			Date: 02/07/2003


FULL PRODUCT VERSION :
JDK: 1.4.0_01-b03 or 1.4.1_01
JRE: 1.4.1_01

FULL OPERATING SYSTEM VERSION :
Win XP SP1

ADDITIONAL OPERATING SYSTEMS :
Win 2000 SP3
Linux



EXTRA RELEVANT SYSTEM CONFIGURATION :
Internet Explorer 5.5
Internet Explorer 6.0
Linux Mozilla


A DESCRIPTION OF THE PROBLEM :
My applet sends many POST requests (10-20 per second) one
by one (not concurrently) to an IIS server application,
using HttpUrlConnection with Keep-Alive option. When I run
it with AppletViewer, I see (with netstat) a single HTTP
connection which is re-used for each new POST.

However, when I run it under IE with Plugin 1.4.0 or 1.4.1,
netstat shows that each POST creates a new connection.
After the response arrives, this connection goes to
TIME_WAIT state for several minutes and the next POST opens
a brand new connection. After some time I see hundreds of
TIME_WAIT connections.

I got an advice (from experts-exchange) to force using
sun.net rather than sun.plugin, thus avoiding the apparent
plugin bug. I do it as follows:

URL.setURLStreamHandlerFactory(null);
sysProperties.setProperty
("java.protocol.handler.pkgs", "sun.net.www.protocol");

Now a single connection is opened and re-used for all
POSTs, but the applet is not aware that it runs under a
plugin or browser, e.g., it doesn't recognize the IE proxy
settings.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the sample applet given in Source Code below.
The corresponding sample server ISAPI application can be
seen at http://212.29.222.92/Applets/ServerCode.html.



EXPECTED VERSUS ACTUAL BEHAVIOR :
Use netstat to see the connections accumulate.
Run the same with AppletViewer - only 1 connection is
opened.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
============= Applet code ===============
import java.io.*;
import java.net.*;

public class Isapi_Applet extends java.applet.Applet {
URL  m_url;
HttpURLConnection  m_conn;
String result;

public void init() {
   try {
       m_url = new URL("http://212.29.222.92/TestOut/IsapiTest.dll");
       for (int i=1; i<=10; i++) {
           String resp = doPost("REQUEST=DUMMY");
           if (resp.equals("RESPONSE=OK"))
               result = i + ") response OK";
           else
               result = i + ") Unexpected response: " + resp;
           showStatus(result);
           System.out.println(result);
       }
       showStatus("Applet completed");
       Thread.sleep(1000); // Allow time to see last message
   }
   catch (Exception exception) {
           exception.printStackTrace();
   }
}

public String doPost(String postData) throws IOException {
   try {
       m_conn = (HttpURLConnection)m_url.openConnection();
       m_conn.setDoOutput(true);
       m_conn.setRequestProperty("Connection", "Keep-Alive");
       m_conn.setUseCaches(false);
       /* Send request */
       PrintWriter outSock = new PrintWriter(m_conn.getOutputStream());
       outSock.print(postData);
       outSock.close();
       /* Read response and return it */
       BufferedReader inSock = new BufferedReader(new InputStreamReader
(m_conn.getInputStream()));
       String response = inSock.readLine();
       inSock.close();
       return response;
   }
   catch (IOException exception) {
           exception.printStackTrace();
           return null;
   }
}
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Force using sun.net rather than sun.plugin:

URL.setURLStreamHandlerFactory(null);
sysProperties.setProperty
("java.protocol.handler.pkgs", "sun.net.www.protocol");

However, the applet does not recognize browser settings,
e.g., proxy.
(Review ID: 180924) 
======================================================================

Comments
EVALUATION This may have to do with the HTTPS issue Thomas has found. ###@###.### 2003-02-07 ###@###.### 2003-02-25 I have done extensive test for this issue, trying to hit their IIS webserver from my html page, I only saw a couple of HTTP connection created, both using applet and JSSE http code. no difference. Dennis Gu ###@###.### 2003-03-21 After setup an IIS 5.0 webserver and test it again, I did reproduce the bug. In Java plugin, we use default web proxy setting as http connect. This is where the problem came from. After I disable the proxy setting in Java Control panel, JPI only create one http connection and keep-alive is working. When the user use JSSE package instead of JPI classes. JSSE will not have proxy http connection, that is why it is working. When we use proxy http conection, boht in JPI and JSSE, we notice the first connection always result in a exception: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at java.io.BufferedInputStream.fill(BufferedInputStream.java:183) at java.io.BufferedInputStream.read1(BufferedInputStream.java:222) at java.io.BufferedInputStream.read(BufferedInputStream.java:277) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:793) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:719) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:624) at sun.plugin.net.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:399) at old_isapi.doPost(old_isapi.java:47) at old_isapi.init(old_isapi.java:19) at sun.applet.AppletPanel.run(AppletPanel.java:353) at java.lang.Thread.run(Thread.java:534) When it try to connect again, it works. I think that is probably the reason the "keep-alive" won't work, because it drop the connection in first try. It looks like a problem either in JSSE or IIS webserve. Dennis
11-06-2004