Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=48895
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 29 Mar 2010 15:01:14 +0000 (15:01 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 29 Mar 2010 15:01:14 +0000 (15:01 +0000)
Make clearing thread locals optional and disabled by default since it isn't thread-safe

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

java/org/apache/catalina/core/StandardContext.java
java/org/apache/catalina/loader/LocalStrings.properties
java/org/apache/catalina/loader/WebappClassLoader.java
java/org/apache/catalina/loader/WebappLoader.java
webapps/docs/config/context.xml

index 0d6878f..4b50e3b 100644 (file)
@@ -772,7 +772,16 @@ public class StandardContext
      * <code>false</code> will be used. 
      */
     private boolean clearReferencesStopThreads = false;
-
+    
+    /**
+     * Should Tomcat attempt to clear any ThreadLocal objects that are instances
+     * of classes loaded by this class loader. Failure to remove any such
+     * objects will result in a memory leak on web application stop, undeploy or
+     * reload. It is disabled by default since the clearing of the ThreadLocal
+     * objects is not performed in a thread-safe manner.
+     */
+    private boolean clearReferencesThreadLocals = false;
+    
     /**
      * Should the effective web.xml be logged when the context starts?
      */
@@ -2288,6 +2297,34 @@ public class StandardContext
 
 
     /**
+     * Return the clearReferencesThreadLocals flag for this Context.
+     */
+    public boolean getClearReferencesThreadLocals() {
+
+        return (this.clearReferencesThreadLocals);
+
+    }
+
+
+    /**
+     * Set the clearReferencesStopThreads feature for this Context.
+     *
+     * @param clearReferencesStopThreads The new flag value
+     */
+    public void setClearReferencesThreadLocals(
+            boolean clearReferencesThreadLocals) {
+
+        boolean oldClearReferencesThreadLocals =
+            this.clearReferencesThreadLocals;
+        this.clearReferencesThreadLocals = clearReferencesThreadLocals;
+        support.firePropertyChange("clearReferencesStopThreads",
+                                   oldClearReferencesThreadLocals,
+                                   this.clearReferencesThreadLocals);
+
+    }
+
+
+    /**
      * Has this context been initialized?
      */
     public boolean isInitialized() {
index 1c4b040..be1b069 100644 (file)
@@ -38,8 +38,10 @@ webappClassLoader.clearReferencesResourceBundlesCount=Removed [{0}] ResourceBund
 webappClassLoader.clearReferencesResourceBundlesFail=Failed to clear ResourceBundle references for web application [{0}]
 webappClassLoader.clearRmiInfo=Failed to find class sun.rmi.transport.Target to clear context class loader for web application [{0}]. This is expected on non-Sun JVMs.
 webappClassLoader.clearRmiFail=Failed to clear context class loader referenced from sun.rmi.transport.Target for web application [{0}]
-webappClassLoader.clearThreadLocalDebug=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]). The ThreadLocal has been correctly set to null and the key will be removed by GC. However, to simplify the process of tracing memory leaks, the key has been forcibly removed.
-webappClassLoader.clearThreadLocal=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]) and a value of type [{3}] (value [{4}]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed.
+webappClassLoader.clearThreadLocalDebug=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]). The ThreadLocal has been correctly set to null and the key will be removed by GC.
+webappClassLoader.clearThreadLocal=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]) and a value of type [{3}] (value [{4}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
+webappClassLoader.clearThreadLocalDebugClear=To simplify the process of tracing memory leaks, the key has been forcibly removed.
+webappClassLoader.clearThreadLocalClear=To prevent a memory leak, the ThreadLocal has been forcibly removed.
 webappClassLoader.clearThreadLocalFail=Failed to clear ThreadLocal references for web application [{0}]
 webappClassLoader.stopThreadFail=Failed to terminate thread named [{0}] for web application [{1}]
 webappClassLoader.stopTimerThreadFail=Failed to terminate TimerThread named [{0}] for web application [{1}]
index a7527e1..2aee542 100644 (file)
@@ -446,6 +446,15 @@ public class WebappClassLoader
     private boolean clearReferencesStopThreads = false;
 
     /**
+     * Should Tomcat attempt to clear any ThreadLocal objects that are instances
+     * of classes loaded by this class loader. Failure to remove any such
+     * objects will result in a memory leak on web application stop, undeploy or
+     * reload. It is disabled by default since the clearing of the ThreadLocal
+     * objects is not performed in a thread-safe manner.
+     */
+    private boolean clearReferencesThreadLocals = false;
+    
+    /**
      * Should Tomcat call {@link org.apache.juli.logging.LogFactory#release()}
      * when the class loader is stopped? If not specified, the default value
      * of <code>true</code> is used. Changing the default setting is likely to
@@ -676,6 +685,27 @@ public class WebappClassLoader
      }
 
 
+
+
+     /**
+      * Return the clearReferencesThreadLocals flag for this Context.
+      */
+     public boolean getClearReferencesThreadLocals() {
+         return (this.clearReferencesThreadLocals);
+     }
+
+
+     /**
+      * Set the clearReferencesThreadLocals feature for this Context.
+      *
+      * @param clearReferencesThreadLocals The new flag value
+      */
+     public void setClearReferencesThreadLocals(
+             boolean clearReferencesThreadLocals) {
+         this.clearReferencesThreadLocals = clearReferencesThreadLocals;
+     }
+
+
      /**
       * Set the clearReferencesLogFactoryRelease feature for this Context.
       *
@@ -2254,16 +2284,26 @@ public class WebappClassLoader
                                     log.debug(sm.getString(
                                             "webappClassLoader.clearThreadLocalDebug",
                                             args));
+                                    if (clearReferencesThreadLocals) {
+                                        log.debug(sm.getString(
+                                                "webappClassLoader.clearThreadLocalDebugClear"));
+                                    }
                                 }
                             } else {
                                 log.error(sm.getString(
                                         "webappClassLoader.clearThreadLocal",
                                         args));
+                                if (clearReferencesThreadLocals) {
+                                    log.info(sm.getString(
+                                            "webappClassLoader.clearThreadLocalClear"));
+                                }
                             }
-                            if (key == null) {
-                              staleEntriesCount++;
-                            } else {
-                              mapRemove.invoke(map, key);
+                            if (clearReferencesThreadLocals) {
+                                if (key == null) {
+                                  staleEntriesCount++;
+                                } else {
+                                  mapRemove.invoke(map, key);
+                                }
                             }
                         }
                     }
index f1e2485..bda7a70 100644 (file)
@@ -596,6 +596,8 @@ public class WebappLoader extends LifecycleBase
                         ((StandardContext) container).getClearReferencesStatic());
                 classLoader.setClearReferencesStopThreads(
                         ((StandardContext) container).getClearReferencesStopThreads());
+                classLoader.setClearReferencesThreadLocals(
+                        ((StandardContext) container).getClearReferencesThreadLocals());
             }
 
             for (int i = 0; i < repositories.length; i++) {
index ca7f227..679d253 100644 (file)
         default value of <code>false</code> will be used.</p>
       </attribute>
 
+      <attribute name="clearReferencesThreadLocals" required="false">
+        <p>If <code>true</code>, Tomcat attempts to clear any ThreadLocal
+        objects that are instances of classes loaded by this class loader.
+        Failure to remove any such objects will result in a memory leak on web
+        application stop, undeploy or reload.  If not specified, the default
+        value of <code>false</code> will be used since the clearing of the
+        ThreadLocal objects is not performed in a thread-safe manner.</p>
+      </attribute>
+
       <attribute name="processTlds" required="false">
         <p>Whether the context should process TLDs on startup.  The default
         is true.  The false setting is intended for special cases