This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 8fff07ff8388806fddbcaa02c0f8d1873c226015 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 fb93d96b5a..d8fd5ad023 100644 --- a/java/org/apache/catalina/core/AprLifecycleListener.java +++ b/java/org/apache/catalina/core/AprLifecycleListener.java @@ -169,6 +169,7 @@ public class AprLifecycleListener implements LifecycleListener { } private static void terminateAPR() { + Library.terminatePrepare(); Library.terminate(); AprStatus.setAprAvailable(false); AprStatus.setAprInitialized(false); diff --git a/java/org/apache/tomcat/jni/Library.java b/java/org/apache/tomcat/jni/Library.java index 218a101b59..cf05501d54 100644 --- a/java/org/apache/tomcat/jni/Library.java +++ b/java/org/apache/tomcat/jni/Library.java @@ -32,6 +32,8 @@ public final class Library { */ private static Library _instance = null; + private static boolean initialized = false; + private Library() throws Exception { boolean loaded = false; StringBuilder err = new StringBuilder(); @@ -100,12 +102,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 */ @@ -284,7 +300,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 afbae25dc9..555e498fb0 100644 --- a/test/org/apache/tomcat/util/net/TesterSupport.java +++ b/test/org/apache/tomcat/util/net/TesterSupport.java @@ -103,6 +103,7 @@ public final class TesterSupport { Library.initialize(null); available = true; version = SSL.version(); + Library.terminatePrepare(); Library.terminate(); } catch (Exception | LibraryNotFoundError ex) { err = ex.getMessage(); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org