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

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

commit b55fb93c8e36f938dd9cf9d2abcd646f76c3bc52
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Dec 1 23:28:07 2026 +0000

    Ignore a second async error once the first has been processed
    
    asyncError() did not record that an error had been processed when the error
    occurred as asynchronous processing started (the STARTING state), unlike 
every
    other state. A second error reported by another thread therefore replaced 
the
    state that error handling had moved the request to (for example 
MUST_COMPLETE)
    with ERROR. The container thread handling the first error then no longer saw
    the request as completing so did not post-process it, and nothing else did, 
so
    onComplete() was not called and the in-progress async count was never
    decremented. Record that the error has been processed in this state as well.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 java/org/apache/coyote/AsyncStateMachine.java      |  7 +++++
 .../coyote/TestAbstractProcessorAsyncError.java    | 34 +++++++++++++++++++++-
 webapps/docs/changelog.xml                         |  7 +++++
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/coyote/AsyncStateMachine.java 
b/java/org/apache/coyote/AsyncStateMachine.java
index a758579d10..e00bcac788 100644
--- a/java/org/apache/coyote/AsyncStateMachine.java
+++ b/java/org/apache/coyote/AsyncStateMachine.java
@@ -492,6 +492,13 @@ class AsyncStateMachine {
 
         clearNonBlockingListeners();
         if (state == AsyncState.STARTING) {
+            /*
+             * Record that the error has been processed, as for the other 
states, so that a second error reported by
+             * another thread is ignored. Otherwise the second error replaces 
the state that error handling has moved
+             * to (for example MUST_COMPLETE) with ERROR. The container thread 
that is handling the first error then no
+             * longer sees the request as completing, so it does not 
post-process it, and nothing else does.
+             */
+            hasProcessedError = true;
             updateState(AsyncState.MUST_ERROR);
         } else {
             if (hasProcessedError) {
diff --git a/test/org/apache/coyote/TestAbstractProcessorAsyncError.java 
b/test/org/apache/coyote/TestAbstractProcessorAsyncError.java
index 20b9c24ade..3fb8a0d659 100644
--- a/test/org/apache/coyote/TestAbstractProcessorAsyncError.java
+++ b/test/org/apache/coyote/TestAbstractProcessorAsyncError.java
@@ -76,6 +76,36 @@ public class TestAbstractProcessorAsyncError {
     }
 
 
+    /*
+     * Error handling on the container thread has already decided that the 
request is to be completed (MUST_COMPLETE)
+     * when another thread reports an error. The completion must not be 
discarded else the container thread, which is
+     * about to post-process the request, no longer sees it as completing and 
nothing completes the request.
+     */
+    @Test
+    public void testErrorWhenCompletionAlreadyDecided() {
+        TestProcessor processor = new TestProcessor();
+        TestAsyncContextCallback asyncCtxt = new TestAsyncContextCallback();
+
+        processor.action(ActionCode.ASYNC_START, asyncCtxt);
+        // Error handling (on the container thread) while the request is 
starting, then it is completed
+        processor.action(ActionCode.ASYNC_ERROR, null);
+        processor.action(ActionCode.ASYNC_COMPLETE, null);
+        Assert.assertTrue(processor.asyncStateMachine.isCompleting());
+        processor.events.clear();
+
+        // Another thread reports an error
+        processor.setErrorState(ErrorState.CLOSE_NOW, null);
+
+        Assert.assertTrue("The completion must not be discarded", 
processor.asyncStateMachine.isCompleting());
+        Assert.assertEquals("No error event should be generated", 
Collections.<SocketEvent>emptyList(), processor.events);
+
+        // The container thread post-processes the request
+        processor.action(ActionCode.ASYNC_POST_PROCESS, null);
+        Assert.assertEquals("onComplete() must be fired", 1, 
asyncCtxt.completes.get());
+        Assert.assertEquals("The in-progress async count must be decremented", 
1, asyncCtxt.decrements.get());
+    }
+
+
     private static class TestProcessor extends Http11Processor {
 
         private final List<SocketEvent> events = new ArrayList<>();
@@ -109,9 +139,11 @@ public class TestAbstractProcessorAsyncError {
         private final AtomicInteger increments = new AtomicInteger();
         private final AtomicInteger decrements = new AtomicInteger();
 
+        private final AtomicInteger completes = new AtomicInteger();
+
         @Override
         public void fireOnComplete() {
-            // NO-OP
+            completes.incrementAndGet();
         }
 
         @Override
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 6033fc4c1d..b51291a032 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -465,6 +465,13 @@
         was then never decremented, when an error is reported for a request
         whose asynchronous processing has just completed. (markt)
       </fix>
+      <fix>
+        Avoid an asynchronous request not being completed, and the count of
+        in-progress asynchronous requests not being decremented, when a second
+        error is reported by another thread while the container is handling an
+        error that occurred as the request started asynchronous processing.
+        (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


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

Reply via email to