This is an automated email from the ASF dual-hosted git repository. markt-asf pushed a commit to branch 11.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit f0521d4987700bf4171c4f62bc6c09db760ddfde Author: Mark Thomas <[email protected]> AuthorDate: Tue Dec 1 20:33:45 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 2a2c2f2807..5f953f4cdb 100644 --- a/java/org/apache/coyote/AbstractProcessor.java +++ b/java/org/apache/coyote/AbstractProcessor.java @@ -142,10 +142,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 e01634ff30..42f0e75443 100644 --- a/java/org/apache/coyote/AsyncStateMachine.java +++ b/java/org/apache/coyote/AsyncStateMachine.java @@ -464,6 +464,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 95ca30fe63..38f829f7fa 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -452,6 +452,11 @@ buffered output cannot be flushed as asynchronous processing completes, 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]
