This is an automated email from the ASF dual-hosted git repository. markt-asf pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 6b25f6067f9d7111b8fd68d10277b708ddd152a1 Author: Mark Thomas <[email protected]> AuthorDate: Tue Dec 1 20:33:46 2026 +0000 Check the async state and report the error atomically in setErrorState() AbstractProcessor.setErrorState() checked that asynchronous processing was in progress and then told the async state machine about the error. If the processing completed in between, the state machine treated the resulting DISPATCHED state as error handling having moved the request back into an asynchronous state and incremented the in-progress async count. Nothing then completed the request so the count was never decremented. Make the check and the error handling a single operation of the state machine. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- java/org/apache/coyote/AbstractProcessor.java | 6 +- java/org/apache/coyote/AsyncStateMachine.java | 15 +++ .../coyote/TestAbstractProcessorAsyncError.java | 131 +++++++++++++++++++++ webapps/docs/changelog.xml | 5 + 4 files changed, 153 insertions(+), 4 deletions(-) diff --git a/java/org/apache/coyote/AbstractProcessor.java b/java/org/apache/coyote/AbstractProcessor.java index b13076f998..0d38060fa7 100644 --- a/java/org/apache/coyote/AbstractProcessor.java +++ b/java/org/apache/coyote/AbstractProcessor.java @@ -143,10 +143,8 @@ public abstract class AbstractProcessor extends AbstractProcessorLight implement if (t != null) { request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t); } - if (blockIo && isAsync()) { - if (asyncStateMachine.asyncError()) { - processSocketEvent(SocketEvent.ERROR, true); - } + if (blockIo && asyncStateMachine.asyncErrorIfAsync()) { + processSocketEvent(SocketEvent.ERROR, true); } } diff --git a/java/org/apache/coyote/AsyncStateMachine.java b/java/org/apache/coyote/AsyncStateMachine.java index efa3fa0809..a758579d10 100644 --- a/java/org/apache/coyote/AsyncStateMachine.java +++ b/java/org/apache/coyote/AsyncStateMachine.java @@ -467,6 +467,21 @@ class AsyncStateMachine { } + /* + * For callers that have not checked, under the lock, that asynchronous processing is in progress. If the state is + * checked first and asyncError() called afterwards, asynchronous processing may complete in between. asyncError() + * treats the resulting DISPATCHED state as error handling having moved the request back into an asynchronous state + * and increments the in-progress async count but, for these callers, nothing then completes the request and + * decrements the count again. + */ + synchronized boolean asyncErrorIfAsync() { + if (!state.isAsync()) { + return false; + } + return asyncError(); + } + + synchronized boolean asyncError() { Request request = processor.getRequest(); boolean containerThread = (request != null && request.isRequestThread()); diff --git a/test/org/apache/coyote/TestAbstractProcessorAsyncError.java b/test/org/apache/coyote/TestAbstractProcessorAsyncError.java new file mode 100644 index 0000000000..430e1d575f --- /dev/null +++ b/test/org/apache/coyote/TestAbstractProcessorAsyncError.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.coyote; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.coyote.http11.Http11NioProtocol; +import org.apache.coyote.http11.Http11Processor; +import org.apache.tomcat.util.net.SocketEvent; + +/** + * Tests the asynchronous error handling triggered by {@link AbstractProcessor#setErrorState(ErrorState, Throwable)}, + * which may be called on any thread, including while asynchronous processing completes on a container thread. + */ +public class TestAbstractProcessorAsyncError { + + @Test + public void testErrorWhileAsyncInProgress() { + TestProcessor processor = new TestProcessor(); + TestAsyncContextCallback asyncCtxt = new TestAsyncContextCallback(); + + processor.action(ActionCode.ASYNC_START, asyncCtxt); + processor.action(ActionCode.ASYNC_POST_PROCESS, null); + + processor.setErrorState(ErrorState.CLOSE_NOW, null); + + Assert.assertEquals("The error must be passed to a container thread", List.of(SocketEvent.ERROR), + processor.events); + } + + + /* + * The asynchronous processing completes after setErrorState() has decided that it is in progress but before the + * async state machine is told about the error. The completion has to win: nothing completes the request again so + * incrementing the in-progress async count would leak it. + */ + @Test + public void testErrorAfterAsyncCompleted() { + TestProcessor processor = new TestProcessor(); + TestAsyncContextCallback asyncCtxt = new TestAsyncContextCallback(); + + processor.action(ActionCode.ASYNC_START, asyncCtxt); + processor.action(ActionCode.ASYNC_POST_PROCESS, null); + processor.action(ActionCode.ASYNC_COMPLETE, null); + processor.action(ActionCode.ASYNC_POST_PROCESS, null); + processor.events.clear(); + Assert.assertEquals(1, asyncCtxt.decrements.get()); + + // Simulates the state having been checked before the processing completed + processor.staleAsync = true; + processor.setErrorState(ErrorState.CLOSE_NOW, null); + + Assert.assertEquals("The in-progress async count must not be incremented", 0, asyncCtxt.increments.get()); + Assert.assertEquals("No error event should be generated for a completed request", List.of(), + processor.events); + } + + + private static class TestProcessor extends Http11Processor { + + private final List<SocketEvent> events = new ArrayList<>(); + private volatile boolean staleAsync = false; + + TestProcessor() { + super(new Http11NioProtocol(), null); + } + + @Override + public boolean isAsync() { + return staleAsync || super.isAsync(); + } + + @Override + protected boolean flushBufferedWrite() { + // No socket is available and nothing is buffered + return false; + } + + @Override + protected void processSocketEvent(SocketEvent event, boolean dispatch) { + // Record the event rather than passing it on. No socket, endpoint or executor is available. + events.add(event); + } + } + + + private static class TestAsyncContextCallback implements AsyncContextCallback { + + private final AtomicInteger increments = new AtomicInteger(); + private final AtomicInteger decrements = new AtomicInteger(); + + @Override + public void fireOnComplete() { + // NO-OP + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public void incrementInProgressAsyncCount() { + increments.incrementAndGet(); + } + + @Override + public void decrementInProgressAsyncCount() { + decrements.incrementAndGet(); + } + } +} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index e0e2ae868e..f86454bb6c 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -453,6 +453,11 @@ performed is completed, for example because the client has reset the HTTP/2 stream. (markt) </fix> + <fix> + Avoid incrementing the count of in-progress asynchronous requests, which + was then never decremented, when an error is reported for a request + whose asynchronous processing has just completed. (markt) + </fix> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
