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
commit b57631dd130e266b3ffe231a3075bbf8022de82d Author: Mark Thomas <ma...@apache.org> AuthorDate: Thu Feb 8 18:47:07 2024 +0000 Preparatory work for APR/Native shutdown stability fix --- .../apache/catalina/core/AprLifecycleListener.java | 1 + java/org/apache/tomcat/jni/Library.java | 32 ++++++++++++++++++---- test/org/apache/tomcat/util/net/TesterSupport.java | 1 + 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/java/org/apache/catalina/core/AprLifecycleListener.java b/java/org/apache/catalina/core/AprLifecycleListener.java index 42a5bf432b..e510d0bdad 100644 --- a/java/org/apache/catalina/core/AprLifecycleListener.java +++ b/java/org/apache/catalina/core/AprLifecycleListener.java @@ -175,6 +175,7 @@ public class AprLifecycleListener implements LifecycleListener { } private static void terminateAPR() { + Library.terminatePrepare(); Library.terminate(); aprAvailable = false; aprInitialized = false; diff --git a/java/org/apache/tomcat/jni/Library.java b/java/org/apache/tomcat/jni/Library.java index 75a2de866d..8c48732740 100644 --- a/java/org/apache/tomcat/jni/Library.java +++ b/java/org/apache/tomcat/jni/Library.java @@ -30,6 +30,8 @@ public final class Library { */ private static Library _instance = null; + private static boolean initialized = false; + private Library() throws Exception { boolean loaded = false; String path = System.getProperty("java.library.path"); @@ -74,12 +76,26 @@ public final class Library { System.loadLibrary(libraryName); } - /* create global TCN's APR pool - * This has to be the first call to TCN library. + /** + * Create Tomcat Native's global APR pool. This has to be the first call to TCN library. */ private static native boolean initialize(); - /* destroy global TCN's APR pool - * This has to be the last call to TCN library. + /** + * Signal that Tomcat Native is about to be shutdown. + * <p> + * The main purpose of this flag is to allow instances that manage their own APR root pools to determine if those + * pools need to be explicitly cleaned up or if they will be / have been cleaned up by the call to + * {@link #terminate()}. The code needs to avoid multiple attempts to clean up these pools else the Native code may + * crash. + */ + public static void terminatePrepare() { + initialized = false; + } + /** + * Destroys Tomcat Native's global APR pool. This has to be the last call to TCN library. This will destroy any APR + * root pools that have not been explicitly destroyed. + * <p> + * Callers of this method should call {@link #terminatePrepare()} before calling this method. */ public static native void terminate(); /* Internal function for loading APR Features */ @@ -247,7 +263,13 @@ public final class Library { throw new UnsatisfiedLinkError("Missing threading support from APR"); } } - return initialize(); + initialized = initialize(); + return initialized; + } + + + public static boolean isInitialized() { + return initialized; } /** diff --git a/test/org/apache/tomcat/util/net/TesterSupport.java b/test/org/apache/tomcat/util/net/TesterSupport.java index 568ea1ece8..c0f908ffc9 100644 --- a/test/org/apache/tomcat/util/net/TesterSupport.java +++ b/test/org/apache/tomcat/util/net/TesterSupport.java @@ -101,6 +101,7 @@ public final class TesterSupport { Library.initialize(null); available = true; version = SSL.version(); + Library.terminatePrepare(); Library.terminate(); } catch (Exception | LibraryNotFoundError ex) { // Ignore --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org