Author: markt Date: Tue May 17 20:36:54 2016 New Revision: 1744323 URL: http://svn.apache.org/viewvc?rev=1744323&view=rev Log: Make checking for RMI Target memory leaks optional and log a warning if running on Java 9 without the necessary command line options
Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java tomcat/trunk/webapps/docs/changelog.xml tomcat/trunk/webapps/docs/config/context.xml Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1744323&r1=1744322&r2=1744323&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Tue May 17 20:36:54 2016 @@ -706,6 +706,13 @@ public class StandardContext extends Con private JarScanner jarScanner = null; /** + * Enables the RMI Target memory leak detection to be controlled. This is + * necessary since the detection can only work on Java 9 if some of the + * modularity checks are disabled. + */ + private boolean clearReferencesRmiTargets = true; + + /** * Should Tomcat attempt to null out any static or final fields from loaded * classes when a web application is stopped as a work around for apparent * garbage collection bugs and application coding errors? There have been @@ -2569,6 +2576,19 @@ public class StandardContext extends Con } + public boolean getClearReferencesRmiTargets() { + return this.clearReferencesRmiTargets; + } + + + public void setClearReferencesRmiTargets(boolean clearReferencesRmiTargets) { + boolean oldClearReferencesRmiTargets = this.clearReferencesRmiTargets; + this.clearReferencesRmiTargets = clearReferencesRmiTargets; + support.firePropertyChange("clearReferencesRmiTargets", + oldClearReferencesRmiTargets, this.clearReferencesRmiTargets); + } + + /** * @return the clearReferencesStatic flag for this Context. */ @@ -5046,6 +5066,8 @@ public class StandardContext extends Con // since the loader just started, the webapp classloader is now // created. + setClassLoaderProperty("clearReferencesRmiTargets", + getClearReferencesRmiTargets()); setClassLoaderProperty("clearReferencesStatic", getClearReferencesStatic()); setClassLoaderProperty("clearReferencesStopThreads", Modified: tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml?rev=1744323&r1=1744322&r2=1744323&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml (original) +++ tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml Tue May 17 20:36:54 2016 @@ -82,6 +82,10 @@ description="Object names of all children" type="[Ljavax.management.ObjectName;"/> + <attribute name="clearReferencesRmiTargets" + description="Should Tomcat look for memory leaks in RMI Targets and clear them if found as a work around for application coding errors?" + type="boolean"/> + <attribute name="clearReferencesStatic" description="Should Tomcat attempt to null out any static or final fields from loaded classes when a web application is stopped as a work around for apparent garbage collection bugs and application coding errors?" type="boolean"/> Modified: tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties?rev=1744323&r1=1744322&r2=1744323&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties Tue May 17 20:36:54 2016 @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +webappClassLoader.addExports=When running on Java 9 you need to add "-XaddExports:java.rmi/sun.rmi.transport=ALL-UNNAMED" to the JVM command line arguments to enable RMI Target memory leak detection. Alternatively, you can suppress this warning by disabling RMI Target memory leak detection. webappClassLoader.addPermisionNoCanonicalFile=Unable to obtain a canonical file path from the URL [{0}] webappClassLoader.addPermisionNoProtocol=The protocol [{0}] in the URL [{1}] is not supported so no read permission was granted for resources located at this URL webappClassLoader.illegalJarPath=Illegal JAR entry detected with name {0} Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java?rev=1744323&r1=1744322&r2=1744323&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java Tue May 17 20:36:54 2016 @@ -77,6 +77,7 @@ import org.apache.juli.logging.LogFactor import org.apache.tomcat.InstrumentableClassLoader; import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.IntrospectionUtils; +import org.apache.tomcat.util.compat.JreCompat; import org.apache.tomcat.util.compat.JreVendor; import org.apache.tomcat.util.res.StringManager; @@ -328,6 +329,13 @@ public abstract class WebappClassLoaderB /** + * Enables the RMI Target memory leak detection to be controlled. This is + * necessary since the detection can only work on Java 9 if some of the + * modularity checks are disabled. + */ + private boolean clearReferencesRmiTargets = true; + + /** * Should Tomcat attempt to null out any static or final fields from loaded * classes when a web application is stopped as a work around for apparent * garbage collection bugs and application coding errors? There have been @@ -521,6 +529,16 @@ public abstract class WebappClassLoaderB } + public boolean getClearReferencesRmiTargets() { + return this.clearReferencesRmiTargets; + } + + + public void setClearReferencesRmiTargets(boolean clearReferencesRmiTargets) { + this.clearReferencesRmiTargets = clearReferencesRmiTargets; + } + + /** * Return the clearReferencesStatic flag for this Context. * @return <code>true</code> if the classloader should attempt to set to null @@ -1529,7 +1547,9 @@ public abstract class WebappClassLoaderB checkThreadLocalsForLeaks(); // Clear RMI Targets loaded by this class loader - clearReferencesRmiTargets(); + if (clearReferencesRmiTargets) { + clearReferencesRmiTargets(); + } // Null out any static or final fields from loaded classes, // as a workaround for apparent garbage collection bugs @@ -2274,6 +2294,16 @@ public abstract class WebappClassLoaderB IllegalAccessException e) { log.warn(sm.getString("webappClassLoader.clearRmiFail", getContextName()), e); + } catch (Exception e) { + JreCompat jreCompat = JreCompat.getInstance(); + if (jreCompat.isInstanceOfInaccessibleObjectException(e)) { + // Must be running on Java 9 without the necessary command line + // options. + log.warn(sm.getString("webappClassLoader.addExports")); + } else { + // Re-throw all other exceptions + throw e; + } } } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1744323&r1=1744322&r2=1744323&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Tue May 17 20:36:54 2016 @@ -51,7 +51,9 @@ RMI Target related memory leaks are avoidable which makes them an application bug that needs to be fixed rather than a JRE bug to work around. Therefore, start logging RMI Target related memory leaks on web - application stop. (markt) + application stop. Add an option that controls if the check for these + leaks is made. Log a warning if running on Java 9 with this check + enabled but without the command line option it requires. (markt) </fix> <fix> Ensure NPE will not be thrown during deployment when scanning jar files Modified: tomcat/trunk/webapps/docs/config/context.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/context.xml?rev=1744323&r1=1744322&r2=1744323&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/context.xml (original) +++ tomcat/trunk/webapps/docs/config/context.xml Tue May 17 20:36:54 2016 @@ -677,7 +677,7 @@ appBase for its Host.</p> </attribute> - <attribute name="clearReferencesHttpClientKeepAliveThread" required = "false"> + <attribute name="clearReferencesHttpClientKeepAliveThread" required="false"> <p>If <code>true</code> and an <code>sun.net.www.http.HttpClient</code> keep-alive timer thread has been started by this web application and is still running, Tomcat will change the context class loader for that @@ -688,7 +688,17 @@ not specified, the default value of <code>true</code> will be used.</p> </attribute> - <attribute name="clearReferencesStatic" required = "false"> + <attribute name="clearReferencesRmiTargets" required="false"> + <p>If <code>true</code>, Tomcat looks for memory leaks associated with + RMI Targets and clears any it finds. This feature uses reflection to + identify the leaks and therefore requires that the command line option + <code>-XaddExports:java.rmi/sun.rmi.transport=ALL-UNNAMED</code> is set + when running on Java 9 and above. Applications without memory leaks + should operate correctly with this attribute set to <code>false</code>. + If not specified, the default value of <code>true</code> will be used.</p> + </attribute> + + <attribute name="clearReferencesStatic" required="false"> <p>If <code>true</code>, Tomcat attempts to null out any static or final fields from loaded classes when a web application is stopped as a work around for apparent garbage collection bugs and application coding @@ -699,7 +709,7 @@ <code>false</code> will be used.</p> </attribute> - <attribute name="clearReferencesStopThreads" required = "false"> + <attribute name="clearReferencesStopThreads" required="false"> <p>If <code>true</code>, Tomcat attempts to terminate threads that have been started by the web application. Stopping threads is performed via the deprecated (for good reason) <code>Thread.stop()</code> method and @@ -712,7 +722,7 @@ <code>Thread.stop()</code> is called on any remaining threads.</p> </attribute> - <attribute name="clearReferencesStopTimerThreads" required = "false"> + <attribute name="clearReferencesStopTimerThreads" required="false"> <p>If <code>true</code>, Tomcat attempts to terminate <code>java.util.Timer</code> threads that have been started by the web application. Unlike standard threads, timer threads can be stopped --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org