bug 50556: improve JreMemoryLeakPreventionListener against leak caused by LdapPoolManager
authorslaurent <slaurent@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 7 Jan 2011 22:56:30 +0000 (22:56 +0000)
committerslaurent <slaurent@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 7 Jan 2011 22:56:30 +0000 (22:56 +0000)
https://issues.apache.org/bugzilla/show_bug.cgi?id=50556

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1056553 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
java/org/apache/catalina/core/LocalStrings.properties
webapps/docs/changelog.xml
webapps/docs/config/listeners.xml

index 2e3a377..1b2b369 100644 (file)
@@ -35,6 +35,8 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.res.StringManager;
 
+import com.sun.jndi.ldap.LdapPoolManager;
+
 /**
  * Provide a workaround for known places where the Java Runtime environment can
  * cause a memory leak or lock files.
@@ -162,6 +164,20 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
         this.xmlParsingProtection = xmlParsingProtection;
     }
     
+    /**
+     * {@link LdapPoolManager} spawns a thread when it is initialized if the 
+     * system property <code>com.sun.jndi.ldap.connect.pool.timeout</code> is
+     * greater than 0.
+     * That thread inherits the context class loader of the current thread, so
+     * that there my be a web application class loader leak if the web app
+     * is the first to use {@link LdapPoolManager}. 
+     */
+    private boolean ldapPoolProtection = true;
+    public boolean isLdapPoolProtection() { return ldapPoolProtection; }
+    public void setLdapPoolProtection(boolean ldapPoolProtection) {
+        this.ldapPoolProtection = ldapPoolProtection;
+    }
+    
     @Override
     public void lifecycleEvent(LifecycleEvent event) {
         // Initialise these classes when Tomcat starts
@@ -358,6 +374,22 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
                                 e);
                     }
                 }
+                
+                if (ldapPoolProtection) {
+                    try {
+                        Class.forName("com.sun.jndi.ldap.LdapPoolManager");
+                    } catch (ClassNotFoundException e) {
+                        if (System.getProperty("java.vendor").startsWith(
+                                "Sun")) {
+                            log.error(sm.getString(
+                                    "jreLeakListener.ldapPoolManagerFail"), e);
+                        } else {
+                            log.debug(sm.getString(
+                                    "jreLeakListener.ldapPoolManagerFail"), e);
+                        }
+                    }
+                }
+
             } finally {
                 Thread.currentThread().setContextClassLoader(loader);
             }
index e3f0018..ef4f866 100644 (file)
@@ -82,6 +82,7 @@ jreLeakListener.gcDaemonFail=Failed to trigger creation of the GC Daemon thread
 jreLeakListener.jarUrlConnCacheFail=Failed to disable Jar URL connection caching by default
 jreLeakListener.xmlParseFail=Error whilst attempting to prevent memory leaks during XML parsing
 jreLeakListener.authPolicyFail=Error whilst attempting to prevent memory leak in javax.security.auth.Policy class
+jreLeakListener.ldapPoolManagerFail=Failed to trigger creation of the com.sun.jndi.ldap.LdapPoolManager class during Tomcat start to prevent possible memory leaks. This is expected on non-Sun JVMs.
 naming.wsdlFailed=Failed to find wsdl file: {0}
 naming.bindFailed=Failed to bind object: {0}
 naming.jmxRegistrationFailed=Failed to register in JMX: {0}
index e3cf8d0..53c87ab 100644 (file)
       <fix>
         <bug>50554</bug>: Code clean up. (markt)
       </fix>
+      <add>
+        <bug>50556</bug>: Improve JreMemoryLeakPreventionListener to prevent
+        a potential class loader leak caused by a thread spawned when the class
+        <code>com.sun.jndi.ldap.LdapPoolManager</code> is initialized and the 
+        system property <code>com.sun.jndi.ldap.connect.pool.timeout</code> is 
+        set to a value greater than 0. (slaurent)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">
index 009786a..195e3fb 100644 (file)
@@ -270,6 +270,19 @@ service:jmx:rmi://&lt;hostname&gt;:10002/jndi/rmi://&lt;hostname&gt;:10001/jmxrm
         trigger a memory leak on reload. Defaults to <code>true</code>.</p>
       </attribute>
 
+      <attribute name="ldapPoolProtection" required="false">
+        <p>Enables protection so that the PoolCleaner thread started by
+        <code>com.sun.jndi.ldap.LdapPoolManager</code> does not result in a 
+        memory leak. The thread is started the first time the 
+        <code>LdapPoolManager</code> class is used if the system property 
+        <code>com.sun.jndi.ldap.connect.pool.timeout</code> is set to a value
+        greater than 0. Without this protection, if a web application uses this 
+        class the PoolCleaner thread will be configured with the thread's 
+        context class loader set to the web application class loader which in 
+        turn will trigger a memory leak on reload. Defaults to 
+        <code>true</code>.</p>
+      </attribute>
+
       <attribute name="securityLoginConfigurationProtection" required="false">
         <p>Enables protection so that usage of the
         <code>javax.security.auth.login.Configuration</code> class by a web