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

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

commit 96000feb5ee895d7e71717b1a1aef8952735c9e3
Author: opencode <[email protected]>
AuthorDate: Mon Oct 12 12:54:25 2026 +0200

    Reject a SemaphoreValve concurrency lower than one
    
    The setter previously rejected only negative values, so a concurrency of
    zero was accepted. The semaphore then started with no permits and, with
    the default blocking mode, every request waited forever to acquire one,
    exhausting the container threads and making the container unresponsive
    without any configuration error being reported.
    
    Reject a concurrency lower than one, and document the minimum in the
    attribute documentation.
---
 .../org/apache/catalina/valves/SemaphoreValve.java |  8 ++++---
 .../apache/catalina/valves/TestSemaphoreValve.java | 25 ++++++++++++++++++++++
 webapps/docs/config/valve.xml                      |  3 ++-
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/java/org/apache/catalina/valves/SemaphoreValve.java 
b/java/org/apache/catalina/valves/SemaphoreValve.java
index 37f715be15..37e926d5ad 100644
--- a/java/org/apache/catalina/valves/SemaphoreValve.java
+++ b/java/org/apache/catalina/valves/SemaphoreValve.java
@@ -65,7 +65,9 @@ public class SemaphoreValve extends ValveBase {
 
 
     /**
-     * Concurrency level of the semaphore.
+     * Concurrency level of the semaphore. The value must be at least one,
+     * since a semaphore without permits combined with blocking would make
+     * every request wait forever.
      */
     protected int concurrency = 10;
 
@@ -81,10 +83,10 @@ public class SemaphoreValve extends ValveBase {
     /**
      * Set the concurrency level of the semaphore.
      *
-     * @param concurrency the concurrency level
+     * @param concurrency the concurrency level, at least one
      */
     public void setConcurrency(int concurrency) {
-        if (concurrency < 0) {
+        if (concurrency < 1) {
             throw new IllegalArgumentException(
                     sm.getString("semaphoreValve.invalidConcurrency", 
Integer.valueOf(concurrency)));
         }
diff --git a/test/org/apache/catalina/valves/TestSemaphoreValve.java 
b/test/org/apache/catalina/valves/TestSemaphoreValve.java
index 5254f97846..d37a87de20 100644
--- a/test/org/apache/catalina/valves/TestSemaphoreValve.java
+++ b/test/org/apache/catalina/valves/TestSemaphoreValve.java
@@ -297,6 +297,31 @@ public class TestSemaphoreValve extends TomcatBaseTest {
         Assert.assertEquals(429, valve.getHighConcurrencyStatus());
     }
 
+    @Test
+    public void testInvalidConcurrency() {
+        SemaphoreValve valve = new SemaphoreValve();
+
+        // A semaphore with no permits, combined with the default blocking
+        // mode, would make every request wait forever
+        try {
+            valve.setConcurrency(0);
+            Assert.fail("Expected the concurrency of zero to be rejected");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        try {
+            valve.setConcurrency(-1);
+            Assert.fail("Expected a negative concurrency to be rejected");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        // The smallest accepted concurrency level remains usable
+        valve.setConcurrency(1);
+        Assert.assertEquals(1, valve.getConcurrency());
+    }
+
 
     @Test
     public void testFairSemaphore() throws Exception {
diff --git a/webapps/docs/config/valve.xml b/webapps/docs/config/valve.xml
index 8f2a6ae9e4..a8a26d9ae8 100644
--- a/webapps/docs/config/valve.xml
+++ b/webapps/docs/config/valve.xml
@@ -2760,7 +2760,8 @@ RequestHeader unset SSL_CLIENT_ESCAPED_CERT</source>
       </attribute>
 
       <attribute name="concurrency" required="false">
-        <p>Concurrency level of the semaphore. The default value is
+        <p>Concurrency level of the semaphore. The value must be at least
+        one. The default value is
         <strong>10</strong>.</p>
       </attribute>
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to