United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5053708 DNS provider does not cleanup resources properly
JDK-5053708 : DNS provider does not cleanup resources properly

Details
Type:
Bug
Submit Date:
2004-05-27
Status:
Resolved
Updated Date:
2007-04-17
Project Name:
JDK
Resolved Date:
2007-04-17
Component:
core-libs
OS:
generic,solaris_10
Sub-Component:
javax.naming
CPU:
sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0,5.0u4
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:
Duplicate:
Relates:
Relates:

Sub Tasks

Description
The DNS provider context does not cleanup the network resources
upon a close.

This problem was posted on the JNDI forum at java.sun.com

import java.net.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;

public class SocketLeak {
    public static void main(String argv[]) throws Exception {
        Map map = new HashMap();
        Hashtable env= new Hashtable();

        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.dns.DnsContextFactory");
        env.put(Context.PROVIDER_URL,"dns://xxx.xxx.xxx.xxx");
        int count = 0;

        while(true) {
            DirContext ctx = new InitialDirContext(env);
            ctx.close();
            ctx = null;
            count++;
            System.out.println(count);
        }
    }
}


The above program loops for around 1020 times on solaris 8, after which it runs
out of file descriptors, and fails with an exception below.

Exception in thread "main" javax.naming.ConfigurationException.  Root exception is java.net.SocketException: Too many open files
        at java.net.PlainDatagramSocketImpl.datagramSocketCreate(Native Method)
        at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:53)
        at java.net.DatagramSocket.createImpl(DatagramSocket.java:278)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:127)
        at com.sun.jndi.dns.DnsClient.<init>(DnsClient.java:81)
        at com.sun.jndi.dns.Resolver.<init>(Resolver.java:33)
        at com.sun.jndi.dns.DnsContext.<init>(DnsContext.java:78)
        at com.sun.jndi.dns.DnsContextFactory.UrlToContext(DnsContextFactory.java:73)
        at com.sun.jndi.dns.DnsContextFactory.getInitialContext(DnsContextFactory.java:41)
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
        at javax.naming.InitialContext.init(InitialContext.java:219)
        at javax.naming.InitialContext.<init>(InitialContext.java:195)
        at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:80)
        at SocketLeak.main(SocketLeak.java:17)

                                    

Comments
EVALUATION

See comments
                                     
2004-09-02
SUGGESTED FIX

--- DnsContext.java     Thu Mar 29 13:53:55 2007

*** 123,135 ****
        retries = ctx.retries;
        lookupCT = ctx.lookupCT;
      }
  
      public void close() {
!       // When adding anything here, consider whether BindingEnumeration
!       // needs a finalizer.
      }
  
  
      //---------- Environment operations
  
      /*
--- 123,137 ----
        retries = ctx.retries;
        lookupCT = ctx.lookupCT;
      }
  
      public void close() {
!       if (resolver != null) {
!           resolver.close();
!           resolver = null;
        }
+     }
  

--- Resolver.java       Thu Mar 29 13:54:25 2007

*** 44,53 ****
--- 44,54 ----
        dnsClient = new DnsClient(servers, timeout, retries);
      }
  
      public void close() {
        dnsClient.close();
+       dnsClient = null;
      }
                                     
2007-05-03



Hardware and Software, Engineered to Work Together