From 286ac9ae3c400220014ea69dec762068efeb40f4 Mon Sep 17 00:00:00 2001
From: markt sun.net.www.http.HttpClient is triggered by a web
- * application. This first call will start a KeepAlive thread with the
- * thread's context class loader configured to be the web application class
- * loader. Defaults to true.
- */
- private boolean keepAliveProtection = true;
- public boolean isKeepAliveProtection() { return keepAliveProtection; }
- public void setKeepAliveProtection(boolean keepAliveProtection) {
- this.keepAliveProtection = keepAliveProtection;
- }
-
- /**
- * Protect against the memory leak caused when the first call to
* javax.security.auth.Policy is triggered by a web
* application. This first call populate a static variable with a reference
* to the context class loader. Defaults to true.
@@ -252,27 +239,6 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
}
/*
- * When a servlet opens a connection using a URL it will use
- * sun.net.www.http.HttpClient which keeps a static reference to
- * a keep-alive cache which is loaded using the web application
- * class loader.
- */
- if (keepAliveProtection) {
- try {
- Class.forName("sun.net.www.http.HttpClient");
- } catch (ClassNotFoundException e) {
- if (System.getProperty("java.vendor").startsWith(
- "Sun")) {
- log.error(sm.getString(
- "jreLeakListener.keepAliveFail"), e);
- } else {
- log.debug(sm.getString(
- "jreLeakListener.keepAliveFail"), e);
- }
- }
- }
-
- /*
* Calling getPolicy retains a static reference to the context
* class loader.
*/
diff --git a/java/org/apache/catalina/core/StandardContext.java b/java/org/apache/catalina/core/StandardContext.java
index bee2a9fb3..ffe79cb8c 100644
--- a/java/org/apache/catalina/core/StandardContext.java
+++ b/java/org/apache/catalina/core/StandardContext.java
@@ -801,6 +801,16 @@ public class StandardContext extends ContainerBase
private boolean clearReferencesStopTimerThreads = false;
/**
+ * If an HttpClient keep-alive timer thread has been started by this web
+ * application and is still running, should Tomcat change the context class
+ * loader from the current {@link WebappClassLoader} to
+ * {@link WebappClassLoader#parent} to prevent a memory leak? Note that the
+ * keep-alive timer thread will stop on its own once the keep-alives all
+ * expire however, on a busy system that might not happen for some time.
+ */
+ private boolean clearReferencesHttpClientKeepAliveThread = true;
+
+ /**
* Should Tomcat renew the threads of the thread pool when the application
* is stopped to avoid memory leaks because of uncleaned ThreadLocal
* variables. This also requires that the threadRenewalDelay property of the
@@ -2535,6 +2545,28 @@ public class StandardContext extends ContainerBase
}
+ /**
+ * Return the clearReferencesHttpClientKeepAliveThread flag for this
+ * Context.
+ */
+ public boolean getClearReferencesHttpClientKeepAliveThread() {
+ return (this.clearReferencesHttpClientKeepAliveThread);
+ }
+
+
+ /**
+ * Set the clearReferencesHttpClientKeepAliveThread feature for this
+ * Context.
+ *
+ * @param clearReferencesHttpClientKeepAliveThread The new flag value
+ */
+ public void setClearReferencesHttpClientKeepAliveThread(
+ boolean clearReferencesHttpClientKeepAliveThread) {
+ this.clearReferencesHttpClientKeepAliveThread =
+ clearReferencesHttpClientKeepAliveThread;
+ }
+
+
public boolean getRenewThreadsWhenStoppingContext() {
return this.renewThreadsWhenStoppingContext;
}
diff --git a/java/org/apache/catalina/loader/LocalStrings.properties b/java/org/apache/catalina/loader/LocalStrings.properties
index a08a05a8e..8b23ce10f 100644
--- a/java/org/apache/catalina/loader/LocalStrings.properties
+++ b/java/org/apache/catalina/loader/LocalStrings.properties
@@ -50,6 +50,7 @@ webappClassLoader.checkThreadLocalsForLeaks.unknown=Unknown
webappClassLoader.checkThreadLocalsForLeaks=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. Threads are going to be renewed over time to try and avoid a probable memory leak.
webappClassLoader.checkThreadLocalsForLeaksDebug=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.checkThreadLocalsForLeaksFail=Failed to check for ThreadLocal references for web application [{0}]
+webappClassLoader.checkThreadsHttpClient=Found HttpClient keep-alive thread using web application class loader. Fixed by switching thread to the parent class loader.
webappClassLoader.stopThreadFail=Failed to terminate thread named [{0}] for web application [{1}]
webappClassLoader.stopTimerThreadFail=Failed to terminate TimerThread named [{0}] for web application [{1}]
webappClassLoader.validationErrorJarPath=Unable to validate JAR entry with name {0}
diff --git a/java/org/apache/catalina/loader/WebappClassLoader.java b/java/org/apache/catalina/loader/WebappClassLoader.java
index 7fbc043d7..3b7a8ec19 100644
--- a/java/org/apache/catalina/loader/WebappClassLoader.java
+++ b/java/org/apache/catalina/loader/WebappClassLoader.java
@@ -132,8 +132,10 @@ public class WebappClassLoader
private static final Listtrue.
If true and an HttpClient keep-alive timer thread has
+ been started by this web application and is still running, Tomcat will
+ change the context class loader for that thread from the current
+ WebappClassLoader to
+ WebappClassLoader#parent to prevent a memory leak. Note
+ that the keep-alive timer thread will stop on its own once the
+ keep-alives all expire however, on a busy system that might not happen
+ for some time. If not specified, the default value of
+ true will be used.
If true, Tomcat attempts to null out any static or final
fields from loaded classes when a web application is stopped as a work
diff --git a/webapps/docs/config/listeners.xml b/webapps/docs/config/listeners.xml
index cb1cbc809..f47748e9d 100644
--- a/webapps/docs/config/listeners.xml
+++ b/webapps/docs/config/listeners.xml
@@ -260,16 +260,6 @@ service:jmx:rmi://<hostname>:10002/jndi/rmi://<hostname>:10001/jmxrm
startup on non-Sun JVMs. The default is true.
Enables protection so that the KeepAlive thread started by
- sun.net.www.http.HttpClient does not result in a memory
- leak. The thread is started the first time the HttpClient
- class is used. Without this protection, if a web application uses this
- class the KeepAlive 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 true.
Enables protection so that the PoolCleaner thread started by
com.sun.jndi.ldap.LdapPoolManager does not result in a
--
2.11.0