From a22d6c1c23f06bb4dbc8b5c72177ebf27b239482 Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Thu, 16 Aug 2012 10:53:11 +0100 Subject: Close DirectoryContext after receiving results If I leave an Application using Stroke running for a few hours(making periodic connection attempts), the JVM would throw an Exception saying "Too Many Open Files". On doing an "lsof -p ", I noticed that there were number of open sockets in CLOSE_WAIT state and these went up after every attempt to do a connect on CoreClient object. Closing of DirContext object fixes this bug and the number of open sockets does not increase. Test-information: Ran MLC and kept on monitoring the result of "lsof -p ". It would not increase after this patch. diff --git a/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java b/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java index 66a38cc..0fc7976 100644 --- a/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java +++ b/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java @@ -32,8 +32,9 @@ public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery imple Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns:"); + DirContext ctx = null; try { - DirContext ctx = new InitialDirContext(env); + ctx = new InitialDirContext(env); Attributes attrs = ctx.getAttributes(service, new String[]{"SRV"}); Attribute attribute = attrs.get("SRV"); for (int i = 0; attribute != null && i < attribute.size(); i++) { @@ -58,7 +59,15 @@ public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery imple onResult.emit(results); } }); - + //close the context as otherwise this will lead to open sockets in + //CLOSE_WAIT condition + if(ctx != null) { + try { + ctx.close(); + } catch (NamingException e) { + //at least we try to close the context + } + } } } -- cgit v0.10.2-6-g49f6