From 286ac9ae3c400220014ea69dec762068efeb40f4 Mon Sep 17 00:00:00 2001 From: markt Date: Fri, 28 Jan 2011 13:03:57 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=50642 Better fix for HttpClient keep-alive thread triggered memory leaks git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1064652 13f79535-47bb-0310-9956-ffa450edef68 --- .../core/JreMemoryLeakPreventionListener.java | 34 ---------------- java/org/apache/catalina/core/StandardContext.java | 32 +++++++++++++++ .../apache/catalina/loader/LocalStrings.properties | 1 + .../apache/catalina/loader/WebappClassLoader.java | 47 +++++++++++++++++++++- java/org/apache/catalina/loader/WebappLoader.java | 2 + webapps/docs/changelog.xml | 6 +++ webapps/docs/config/context.xml | 12 ++++++ webapps/docs/config/listeners.xml | 10 ----- 8 files changed, 98 insertions(+), 46 deletions(-) diff --git a/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java b/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java index 942a69ae1..e646c8f39 100644 --- a/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java +++ b/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java @@ -82,19 +82,6 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener { /** * Protect against the memory leak caused when the first call to - * 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 List JVM_THREAD_GROUP_NAMES = new ArrayList(); + private static final String JVN_THREAD_GROUP_SYSTEM = "system"; + static { - JVM_THREAD_GROUP_NAMES.add("system"); + JVM_THREAD_GROUP_NAMES.add(JVN_THREAD_GROUP_SYSTEM); JVM_THREAD_GROUP_NAMES.add("RMI Runtime"); } @@ -465,6 +467,15 @@ public class WebappClassLoader */ private boolean clearReferencesLogFactoryRelease = true; + /** + * 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; /** * Name of associated context used with logging and JMX to associate with @@ -745,6 +756,28 @@ public class WebappClassLoader } + /** + * 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; + } + + // ------------------------------------------------------- Reloader Methods @@ -2166,10 +2199,20 @@ public class WebappClassLoader continue; } - // Don't warn about JVM controlled threads + // JVM controlled threads ThreadGroup tg = thread.getThreadGroup(); if (tg != null && JVM_THREAD_GROUP_NAMES.contains(tg.getName())) { + + // HttpClient keep-alive threads + if (clearReferencesHttpClientKeepAliveThread && + thread.getName().equals("Keep-Alive-Timer")) { + thread.setContextClassLoader(parent); + log.debug(sm.getString( + "webappClassLoader.checkThreadsHttpClient")); + } + + // Don't warn about remaining JVM controlled threads continue; } diff --git a/java/org/apache/catalina/loader/WebappLoader.java b/java/org/apache/catalina/loader/WebappLoader.java index 0c920c136..197347899 100644 --- a/java/org/apache/catalina/loader/WebappLoader.java +++ b/java/org/apache/catalina/loader/WebappLoader.java @@ -591,6 +591,8 @@ public class WebappLoader extends LifecycleMBeanBase ((StandardContext) container).getClearReferencesStopThreads()); classLoader.setClearReferencesStopTimerThreads( ((StandardContext) container).getClearReferencesStopTimerThreads()); + classLoader.setClearReferencesHttpClientKeepAliveThread( + ((StandardContext) container).getClearReferencesHttpClientKeepAliveThread()); } for (int i = 0; i < repositories.length; i++) { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index f21e2f47d..e36a09ee2 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -98,6 +98,12 @@ Use getName() instead of logName() in error messages in StandardContext. (kkolinko) + + 50642: Move the HttpClient keep-alive thread memory leak + protection from the JreMemoryLeakPreventionListener to the + WebappClassLoader since the thread that triggers the memory leak is + created on demand. (markt) + diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml index 058424f09..2465bd185 100644 --- a/webapps/docs/config/context.xml +++ b/webapps/docs/config/context.xml @@ -503,6 +503,18 @@ of the flag is true.

+ +

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