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

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

commit 2001d59f5a326696ebd6ea7f26f83113f97803ef
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 8a6184438d..4ed37531fa 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 9b9585f689..f7a72a9606 100644
--- a/java/org/apache/tomcat/jni/Library.java
+++ b/java/org/apache/tomcat/jni/Library.java
@@ -29,6 +29,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();
@@ -97,12 +99,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 */
@@ -161,6 +177,12 @@ public final class Library {
                                                aprVersionString() + ")");
             }
         }
-        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 03eb1d9bb9..5bf884b393 100644
--- a/test/org/apache/tomcat/util/net/TesterSupport.java
+++ b/test/org/apache/tomcat/util/net/TesterSupport.java
@@ -105,6 +105,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

Reply via email to