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 de6aacea433f28786329ac14f32d41a4d9951ebb
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu May 4 14:41:01 2023 +0100

    Move management of utility executor from init/destroy to start/stop
---
 java/org/apache/catalina/connector/Connector.java  | 14 ++++++++---
 java/org/apache/catalina/core/ContainerBase.java   | 20 +++++++---------
 java/org/apache/catalina/core/StandardServer.java  | 28 +++++++++++-----------
 .../apache/catalina/ha/tcp/SimpleTcpCluster.java   |  5 +++-
 webapps/docs/changelog.xml                         |  5 ++++
 5 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/java/org/apache/catalina/connector/Connector.java 
b/java/org/apache/catalina/connector/Connector.java
index c504210b64..7339f961b2 100644
--- a/java/org/apache/catalina/connector/Connector.java
+++ b/java/org/apache/catalina/connector/Connector.java
@@ -982,9 +982,6 @@ public class Connector extends LifecycleMBeanBase {
         // Initialize adapter
         adapter = new CoyoteAdapter(this);
         protocolHandler.setAdapter(adapter);
-        if (service != null) {
-            
protocolHandler.setUtilityExecutor(service.getServer().getUtilityExecutor());
-        }
 
         // Make sure parseBodyMethodsSet has a default
         if (null == parseBodyMethodsSet) {
@@ -1033,9 +1030,15 @@ public class Connector extends LifecycleMBeanBase {
 
         setState(LifecycleState.STARTING);
 
+        // Configure the utility executor before starting the protocol handler
+        if (protocolHandler != null && service != null) {
+            
protocolHandler.setUtilityExecutor(service.getServer().getUtilityExecutor());
+        }
+
         try {
             protocolHandler.start();
         } catch (Exception e) {
+            // Includes NPE - protocolHandler will be null for invalid 
protocol if throwOnFailure is false
             throw new 
LifecycleException(sm.getString("coyoteConnector.protocolHandlerStartFailed"), 
e);
         }
     }
@@ -1058,6 +1061,11 @@ public class Connector extends LifecycleMBeanBase {
         } catch (Exception e) {
             throw new 
LifecycleException(sm.getString("coyoteConnector.protocolHandlerStopFailed"), 
e);
         }
+
+        // Remove the utility executor once the protocol handler has been 
stopped
+        if (protocolHandler != null) {
+            protocolHandler.setUtilityExecutor(null);
+        }
     }
 
 
diff --git a/java/org/apache/catalina/core/ContainerBase.java 
b/java/org/apache/catalina/core/ContainerBase.java
index 7c96f5de5e..c5bc5994a6 100644
--- a/java/org/apache/catalina/core/ContainerBase.java
+++ b/java/org/apache/catalina/core/ContainerBase.java
@@ -820,13 +820,6 @@ public abstract class ContainerBase extends 
LifecycleMBeanBase implements Contai
     }
 
 
-    @Override
-    protected void initInternal() throws LifecycleException {
-        reconfigureStartStopExecutor(getStartStopThreads());
-        super.initInternal();
-    }
-
-
     private void reconfigureStartStopExecutor(int threads) {
         if (threads == 1) {
             // Use a fake executor
@@ -852,6 +845,8 @@ public abstract class ContainerBase extends 
LifecycleMBeanBase implements Contai
     @Override
     protected synchronized void startInternal() throws LifecycleException {
 
+        reconfigureStartStopExecutor(getStartStopThreads());
+
         // Start our subordinate components, if any
         logger = null;
         getLogger();
@@ -958,6 +953,12 @@ public abstract class ContainerBase extends 
LifecycleMBeanBase implements Contai
         if (cluster instanceof Lifecycle) {
             ((Lifecycle) cluster).stop();
         }
+
+        // If init fails, this may be null
+        if (startStopExecutor != null) {
+            startStopExecutor.shutdownNow();
+            startStopExecutor = null;
+        }
     }
 
     @Override
@@ -987,11 +988,6 @@ public abstract class ContainerBase extends 
LifecycleMBeanBase implements Contai
             parent.removeChild(this);
         }
 
-        // If init fails, this may be null
-        if (startStopExecutor != null) {
-            startStopExecutor.shutdownNow();
-        }
-
         super.destroyInternal();
     }
 
diff --git a/java/org/apache/catalina/core/StandardServer.java 
b/java/org/apache/catalina/core/StandardServer.java
index f7ac2d2e61..00e70c8b48 100644
--- a/java/org/apache/catalina/core/StandardServer.java
+++ b/java/org/apache/catalina/core/StandardServer.java
@@ -909,6 +909,12 @@ public final class StandardServer extends 
LifecycleMBeanBase implements Server {
         fireLifecycleEvent(CONFIGURE_START_EVENT, null);
         setState(LifecycleState.STARTING);
 
+        // Initialize utility executor
+        synchronized (utilityExecutorLock) {
+            
reconfigureUtilityExecutor(getUtilityThreadsInternal(utilityThreads));
+            register(utilityExecutor, "type=UtilityExecutor");
+        }
+
         globalNamingResources.start();
 
         // Start our defined Services
@@ -969,6 +975,14 @@ public final class StandardServer extends 
LifecycleMBeanBase implements Server {
             service.stop();
         }
 
+        synchronized (utilityExecutorLock) {
+            if (utilityExecutor != null) {
+                utilityExecutor.shutdownNow();
+                unregister("type=UtilityExecutor");
+                utilityExecutor = null;
+            }
+        }
+
         globalNamingResources.stop();
 
         stopAwait();
@@ -983,12 +997,6 @@ public final class StandardServer extends 
LifecycleMBeanBase implements Server {
 
         super.initInternal();
 
-        // Initialize utility executor
-        synchronized (utilityExecutorLock) {
-            
reconfigureUtilityExecutor(getUtilityThreadsInternal(utilityThreads));
-            register(utilityExecutor, "type=UtilityExecutor");
-        }
-
         // Register global String cache
         // Note although the cache is global, if there are multiple Servers
         // present in the JVM (may happen when embedding) then the same cache
@@ -1047,14 +1055,6 @@ public final class StandardServer extends 
LifecycleMBeanBase implements Server {
 
         unregister(onameStringCache);
 
-        synchronized (utilityExecutorLock) {
-            if (utilityExecutor != null) {
-                utilityExecutor.shutdownNow();
-                unregister("type=UtilityExecutor");
-                utilityExecutor = null;
-            }
-        }
-
         super.destroyInternal();
     }
 
diff --git a/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java 
b/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java
index 83b27bddac..3f01ebf0eb 100644
--- a/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java
+++ b/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java
@@ -530,7 +530,6 @@ public class SimpleTcpCluster extends LifecycleMBeanBase
             name.append(",component=Deployer");
             onameClusterDeployer = register(clusterDeployer, name.toString());
         }
-        
channel.setUtilityExecutor(Container.getService(getContainer()).getServer().getUtilityExecutor());
     }
 
 
@@ -548,6 +547,8 @@ public class SimpleTcpCluster extends LifecycleMBeanBase
             log.info(sm.getString("simpleTcpCluster.start"));
         }
 
+        
channel.setUtilityExecutor(Container.getService(getContainer()).getServer().getUtilityExecutor());
+
         try {
             checkDefaults();
             registerClusterValve();
@@ -655,6 +656,8 @@ public class SimpleTcpCluster extends LifecycleMBeanBase
         } catch (Exception x) {
             log.error(sm.getString("simpleTcpCluster.stopUnable"), x);
         }
+
+        channel.setUtilityExecutor(null);
     }
 
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index aa60bbafb9..0514133e5e 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -111,6 +111,11 @@
         Add RateLimitFilter which can be used to mitigate DoS and Brute Force
         attacks. (isapir)
       </add>
+      <scode>
+        Move the management of the utility executor from the
+        <code>init()</code>/<code>destroy()</code> methods of components to the
+        <code>start()</code>/<code>stop()</code> methods. (markt)
+      </scode>
     </changelog>
   </subsection>
   <subsection name="Coyote">


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

Reply via email to