Connection leak in hessian

From: Steen Jansdal <steen@xxx.dk>
Date: Tue Dec 07 2004 - 00:21:13 PST

Hi,

While doing a performance test of hessian I discovered a leak.

I wrote a very simple server and a simple client with the purpose
of testing how long 100000 method invocations would take.
After approx. 5000 invocations I suddenly got an exception:

java.net.BindException: Address already in use: connect
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
  at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
  at java.net.Socket.connect(Socket.java:452)
  at java.net.Socket.connect(Socket.java:402)
  at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
  at sun.net.www.http.HttpClient.openServer(HttpClient.java:402)
  at sun.net.www.http.HttpClient.openServer(HttpClient.java:618)
  at sun.net.www.http.HttpClient.<init>(HttpClient.java:306)
  at sun.net.www.http.HttpClient.<init>(HttpClient.java:267)
  at sun.net.www.http.HttpClient.New(HttpClient.java:339)
  at sun.net.www.http.HttpClient.New(HttpClient.java:320)
  at sun.net.www.http.HttpClient.New(HttpClient.java:315)
  at
sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:521)
  at
sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:498)
  at
sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:569)
  at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:123)
  at $Proxy0.getUser(Unknown Source)
  at dk.jansdal.hessian.Client.main(Client.java:38)
        
        
By digging into the code I found that the connection to the server
wasn't closed after each invocation. Only the garbage collector closed
these connections but my code was faster than the garbage collector were
able to close them.

By explicitly closing the connection after each invocation the
problem went away.

In HessianProxy.java:

Exchange
     try {
       URLConnection conn = _factory.openConnection(_url);
       conn.setRequestProperty("Content-Type", "text/xml");
with
     try {
       URLConnection conn = _factory.openConnection(_url);
       try {
         conn.setRequestProperty("Content-Type", "text/xml");

and exchange
       return in.readReply(method.getReturnType());
     } catch (HessianProtocolException e) {
       e.printStackTrace();

with
         return in.readReply(method.getReturnType());
       } finally {
               ((HttpURLConnection)conn).disconnect();
       }
     } catch (HessianProtocolException e) {
       e.printStackTrace();

Best regards
Steen
Received on Tue 07 Dec 2004 00:21:13 -0800

This archive was generated by hypermail 2.1.8 : Thu Sep 28 2006 - 20:16:40 PDT