You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/05/17 16:18:25 UTC

[tomcat] branch 8.5.x updated: Update thread stopping code for Java 19 changes

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new dd2cc01dc4 Update thread stopping code for Java 19 changes
dd2cc01dc4 is described below

commit dd2cc01dc48da22e4a4989f6cabac3518cb5be77
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue May 17 16:43:19 2022 +0100

    Update thread stopping code for Java 19 changes
---
 .../catalina/loader/WebappClassLoaderBase.java     |  42 ++-------
 .../org/apache/tomcat/util/compat/Jre19Compat.java |  84 +++++++++++++++++
 java/org/apache/tomcat/util/compat/JreCompat.java  | 102 +++++++++++++++++----
 .../tomcat/util/compat/LocalStrings.properties     |   2 +
 webapps/docs/changelog.xml                         |   8 ++
 5 files changed, 187 insertions(+), 51 deletions(-)

diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
index e6d0707c02..8b0ce5ca08 100644
--- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java
+++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
@@ -1826,41 +1826,13 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
                     // shutting down the executor
                     boolean usingExecutor = false;
                     try {
-
-                        // Runnable wrapped by Thread
-                        // "target" in Sun/Oracle JDK
-                        // "runnable" in IBM JDK
-                        // "action" in Apache Harmony
-                        Object target = null;
-                        for (String fieldName : new String[] { "target", "runnable", "action" }) {
-                            try {
-                                Field targetField = thread.getClass().getDeclaredField(fieldName);
-                                targetField.setAccessible(true);
-                                target = targetField.get(thread);
-                                break;
-                            } catch (NoSuchFieldException nfe) {
-                                continue;
-                            }
-                        }
-
-                        // "java.util.concurrent" code is in public domain,
-                        // so all implementations are similar including our
-                        // internal fork.
-                        if (target != null && target.getClass().getCanonicalName() != null &&
-                                (target.getClass().getCanonicalName().equals(
-                                        "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
-                                        target.getClass().getCanonicalName().equals(
-                                                "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
-                            Field executorField = target.getClass().getDeclaredField("this$0");
-                            executorField.setAccessible(true);
-                            Object executor = executorField.get(target);
-                            if (executor instanceof ThreadPoolExecutor) {
-                                ((ThreadPoolExecutor) executor).shutdownNow();
-                                usingExecutor = true;
-                            } else if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
-                                ((java.util.concurrent.ThreadPoolExecutor) executor).shutdownNow();
-                                usingExecutor = true;
-                            }
+                        Object executor = JreCompat.getInstance().getExecutor(thread);
+                        if (executor instanceof ThreadPoolExecutor) {
+                            ((ThreadPoolExecutor) executor).shutdownNow();
+                            usingExecutor = true;
+                        } else if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
+                            ((java.util.concurrent.ThreadPoolExecutor) executor).shutdownNow();
+                            usingExecutor = true;
                         }
                     } catch (NoSuchFieldException | IllegalAccessException | RuntimeException e) {
                         // InaccessibleObjectException is only available in Java 9+,
diff --git a/java/org/apache/tomcat/util/compat/Jre19Compat.java b/java/org/apache/tomcat/util/compat/Jre19Compat.java
new file mode 100644
index 0000000000..7f120c4d61
--- /dev/null
+++ b/java/org/apache/tomcat/util/compat/Jre19Compat.java
@@ -0,0 +1,84 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomcat.util.compat;
+
+import java.lang.reflect.Field;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.res.StringManager;
+
+public class Jre19Compat extends Jre9Compat {
+
+    private static final Log log = LogFactory.getLog(Jre19Compat.class);
+    private static final StringManager sm = StringManager.getManager(Jre19Compat.class);
+
+    private static final boolean supported;
+
+    static {
+        // Don't need any Java 19 specific classes (yet) so just test for one of
+        // the new ones for now.
+        Class<?> c1 = null;
+        try {
+            c1 = Class.forName("java.lang.WrongThreadException");
+        } catch (ClassNotFoundException cnfe) {
+            // Must be pre-Java 16
+            log.debug(sm.getString("jre19Compat.javaPre19"), cnfe);
+        }
+
+        supported = (c1 != null);
+    }
+
+    static boolean isSupported() {
+        return supported;
+    }
+
+    @Override
+    public Object getExecutor(Thread thread)
+            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
+
+        Object result = super.getExecutor(thread);
+
+        if (result == null) {
+            Object holder = null;
+            Object task = null;
+            try {
+                Field holderField = thread.getClass().getDeclaredField("holder");
+                holderField.setAccessible(true);
+                holder = holderField.get(thread);
+
+                Field taskField = holder.getClass().getDeclaredField("task");
+                taskField.setAccessible(true);
+                task = taskField.get(holder);
+            } catch (NoSuchFieldException nfe) {
+                return null;
+            }
+
+            if (task!= null && task.getClass().getCanonicalName() != null &&
+                    (task.getClass().getCanonicalName().equals(
+                            "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
+                            task.getClass().getCanonicalName().equals(
+                                    "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
+                Field executorField = task.getClass().getDeclaredField("this$0");
+                executorField.setAccessible(true);
+                result = executorField.get(task);
+            }
+        }
+
+        return result;
+    }
+}
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java
index 162ecccc9f..487f1093ef 100644
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
@@ -19,6 +19,7 @@ package org.apache.tomcat.util.compat;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
@@ -38,33 +39,36 @@ import org.apache.tomcat.util.res.StringManager;
  */
 public class JreCompat {
 
+    private static final StringManager sm = StringManager.getManager(JreCompat.class);
+
     private static final int RUNTIME_MAJOR_VERSION = 7;
 
     private static final JreCompat instance;
-    private static StringManager sm =
-            StringManager.getManager(JreCompat.class.getPackage().getName());
+    private static final boolean jre19Available;
     private static final boolean jre11Available;
     private static final boolean jre9Available;
     private static final boolean jre8Available;
 
 
     static {
-        // This is Tomcat 8 with a minimum Java version of Java 7. The latest
-        // Java version the optional features require is Java 9.
+        // This is Tomcat 8 with a minimum Java version of Java 7.
+        // Compatibility code exists for Java 8, 9, 11 & 19
         // Look for the highest supported JVM first
-        if (Jre9Compat.isSupported()) {
-            instance = new Jre9Compat();
+        if (Jre19Compat.isSupported()) {
+            instance = new Jre19Compat();
+            jre19Available = true;
             jre9Available = true;
             jre8Available = true;
-        }
-        else if (Jre8Compat.isSupported()) {
-            instance = new Jre8Compat();
-            jre9Available = false;
+        } else if (Jre9Compat.isSupported()) {
+            instance = new Jre9Compat();
+            jre19Available = false;
+            jre9Available = true;
             jre8Available = true;
         } else {
             instance = new JreCompat();
-            jre8Available = false;
+            jre19Available = false;
             jre9Available = false;
+            jre8Available = false;
         }
         jre11Available = instance.jarFileRuntimeMajorVersion() >= 11;
     }
@@ -82,11 +86,6 @@ public class JreCompat {
     }
 
 
-    public static boolean isJre11Available() {
-        return jre11Available;
-    }
-
-
     @SuppressWarnings("unused")
     public void setUseServerCipherSuitesOrder(SSLParameters engine, boolean useCipherSuitesOrder) {
         throw new UnsupportedOperationException(sm.getString("jreCompat.noServerCipherSuiteOrder"));
@@ -269,4 +268,75 @@ public class JreCompat {
     public String getModuleName(Class<?> type) {
         return "NO_MODULE_JAVA_8";
     }
+
+
+    // Java 7 implementations of Java 11 methods
+
+    public static boolean isJre11Available() {
+        return jre11Available;
+    }
+
+
+    // Java 7 implementations of Java 19 methods
+
+    public static boolean isJre19Available() {
+        return jre19Available;
+    }
+
+
+    /**
+     * Obtains the executor, if any, used to create the provided thread.
+     *
+     * @param thread    The thread to examine
+     *
+     * @return  The executor, if any, that created the provided thread
+     *
+     * @throws NoSuchFieldException
+     *              If a field used via reflection to obtain the executor cannot
+     *              be found
+     * @throws SecurityException
+     *              If a security exception occurs while trying to identify the
+     *              executor
+     * @throws IllegalArgumentException
+     *              If the instance object does not match the class of the field
+     *              when obtaining a field value via reflection
+     * @throws IllegalAccessException
+     *              If a field is not accessible due to access restrictions
+     */
+    public Object getExecutor(Thread thread)
+            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
+
+        Object result = null;
+
+        // Runnable wrapped by Thread
+        // "target" in Sun/Oracle JDK
+        // "runnable" in IBM JDK
+        // "action" in Apache Harmony
+        Object target = null;
+        for (String fieldName : new String[] { "target", "runnable", "action" }) {
+            try {
+                Field targetField = thread.getClass().getDeclaredField(fieldName);
+                targetField.setAccessible(true);
+                target = targetField.get(thread);
+                break;
+            } catch (NoSuchFieldException nfe) {
+                continue;
+            }
+        }
+
+        // "java.util.concurrent" code is in public domain,
+        // so all implementations are similar including our
+        // internal fork.
+        if (target != null && target.getClass().getCanonicalName() != null &&
+                (target.getClass().getCanonicalName().equals(
+                        "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
+                        target.getClass().getCanonicalName().equals(
+                                "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
+            Field executorField = target.getClass().getDeclaredField("this$0");
+            executorField.setAccessible(true);
+            result = executorField.get(target);
+        }
+
+        return result;
+    }
 }
diff --git a/java/org/apache/tomcat/util/compat/LocalStrings.properties b/java/org/apache/tomcat/util/compat/LocalStrings.properties
index 27e392cbd6..262b81554f 100644
--- a/java/org/apache/tomcat/util/compat/LocalStrings.properties
+++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties
@@ -16,6 +16,8 @@
 jre8Compat.javaPre8=Class not found so assuming code is running on a pre-Java 8 JVM
 jre8Compat.unexpected=Failed to create references to Java 8 classes and methods
 
+jre19Compat.javaPre19=Class not found so assuming code is running on a pre-Java 19 JVM
+
 jre9Compat.invalidModuleUri=The module URI provided [{0}] could not be converted to a URL for the JarScanner to process
 jre9Compat.javaPre9=Class not found so assuming code is running on a pre-Java 9 JVM
 jre9Compat.unexpected=Failed to create references to Java 9 classes and methods
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index befcac5ec8..53a28c5d23 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -105,6 +105,14 @@
   issues do not "pop up" wrt. others).
 -->
 <section name="Tomcat 8.5.80 (schultz)" rtext="in development">
+  <subsection name="Catalina">
+    <changelog>
+      <fix>
+        Update the memory leak protection code to support stopping application
+        created executor threads when running on Java 19 and later. (markt)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="Jasper">
     <changelog>
       <fix>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org