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

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


The following commit(s) were added to refs/heads/main by this push:
     new a6a2dcdb5c Ignore a second async error once the first has been 
processed
a6a2dcdb5c is described below

commit a6a2dcdb5cb6195ed06934252ce49e227dd66652
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Dec 1 23:18:03 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 42f0e75443..c149e24640 100644
--- a/java/org/apache/coyote/AsyncStateMachine.java
+++ b/java/org/apache/coyote/AsyncStateMachine.java
@@ -489,6 +489,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 430e1d575f..4b93091e08 100644
--- a/test/org/apache/coyote/TestAbstractProcessorAsyncError.java
+++ b/test/org/apache/coyote/TestAbstractProcessorAsyncError.java
@@ -75,6 +75,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", List.of(), 
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<>();
@@ -108,9 +138,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 26e12c5e43..b12e665e31 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -587,6 +587,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