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 e833ddd60ba5b034bb1730732e81e8c46ec48cce Author: Mark Thomas <[email protected]> AuthorDate: Tue Dec 1 17:47:51 2026 +0000 Complete async processing even if the buffered output cannot be flushed If flushing buffered output failed when a dispatch that could no longer be performed was completed (for example because the client had reset the HTTP/2 stream), the exception left asyncPostProcess() before onComplete() was fired and before the context's in-progress async count was decremented. Complete the processing first and then re-throw the failure for the caller to handle. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- java/org/apache/coyote/AsyncStateMachine.java | 17 +++- .../coyote/TestAsyncStateMachineFlushFailure.java | 102 +++++++++++++++++++++ webapps/docs/changelog.xml | 7 ++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/java/org/apache/coyote/AsyncStateMachine.java b/java/org/apache/coyote/AsyncStateMachine.java index 3d9c50170f..efa3fa0809 100644 --- a/java/org/apache/coyote/AsyncStateMachine.java +++ b/java/org/apache/coyote/AsyncStateMachine.java @@ -318,12 +318,25 @@ class AsyncStateMachine { // is still allowed (CLOSE_CLEAN) the current response can still be // completed before the connection is closed so write any buffered // output first. - if (processor.getErrorState().isIoAllowed() && processor.flushBufferedWrite()) { - return SocketState.LONG; + /* + * If the write fails (for example because the client has reset the stream) the failure must not prevent + * the request from being completed. onComplete() still has to be called and the in-progress async count + * balanced, so complete first and then re-throw the failure for the caller to handle. + */ + IOException flushFailure = null; + try { + if (processor.getErrorState().isIoAllowed() && processor.flushBufferedWrite()) { + return SocketState.LONG; + } + } catch (IOException ioe) { + flushFailure = ioe; } asyncCtxt.fireOnComplete(); updateState(AsyncState.DISPATCHED); asyncCtxt.decrementInProgressAsyncCount(); + if (flushFailure != null) { + throw flushFailure; + } return SocketState.ASYNC_END; } else if (state == AsyncState.DISPATCHING) { asyncCtxt.fireOnComplete(); diff --git a/test/org/apache/coyote/TestAsyncStateMachineFlushFailure.java b/test/org/apache/coyote/TestAsyncStateMachineFlushFailure.java new file mode 100644 index 0000000000..6ba9ab44b2 --- /dev/null +++ b/test/org/apache/coyote/TestAsyncStateMachineFlushFailure.java @@ -0,0 +1,102 @@ +/* + * 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.io.IOException; +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; + +/** + * Tests that a failure to flush buffered output when a dispatch can no longer be performed, for example because the + * client has reset the stream, does not prevent the request from being completed. + */ +public class TestAsyncStateMachineFlushFailure { + + @Test + public void testFlushFailureWhenDispatchCannotBePerformed() { + TestProcessor processor = new TestProcessor(); + TestAsyncContextCallback asyncCtxt = new TestAsyncContextCallback(); + + processor.action(ActionCode.ASYNC_START, asyncCtxt); + // A dispatch requested on the container thread while the request is starting + processor.getRequest().setRequestThread(); + processor.action(ActionCode.ASYNC_DISPATCH, null); + // The connection will be closed but the response can still be completed + processor.setErrorState(ErrorState.CLOSE_CLEAN, null); + + // Flushing the buffered output fails + processor.action(ActionCode.ASYNC_POST_PROCESS, null); + + Assert.assertEquals("The flush should have been attempted", 1, processor.flushAttempts.get()); + Assert.assertEquals("onComplete() must still be fired", 1, asyncCtxt.completes.get()); + Assert.assertEquals("The in-progress async count must still be decremented", 1, asyncCtxt.decrements.get()); + Assert.assertTrue("The failure must still be handled", processor.getErrorState().isError()); + } + + + private static class TestProcessor extends Http11Processor { + + private final AtomicInteger flushAttempts = new AtomicInteger(); + + TestProcessor() { + super(new Http11NioProtocol(), null); + } + + @Override + protected boolean flushBufferedWrite() throws IOException { + flushAttempts.incrementAndGet(); + throw new CloseNowException("The stream is not writable"); + } + + @Override + protected void processSocketEvent(org.apache.tomcat.util.net.SocketEvent event, boolean dispatch) { + // NO-OP. No socket or endpoint is available. + } + } + + + private static class TestAsyncContextCallback implements AsyncContextCallback { + + private final AtomicInteger completes = new AtomicInteger(); + private final AtomicInteger decrements = new AtomicInteger(); + + @Override + public void fireOnComplete() { + completes.incrementAndGet(); + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public void incrementInProgressAsyncCount() { + // NO-OP + } + + @Override + public void decrementInProgressAsyncCount() { + decrements.incrementAndGet(); + } + } +} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 85e486f1aa..44ab801f7d 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -440,6 +440,13 @@ <code>AsyncContext.complete()</code>, as the request is recycled. (markt) </fix> + <fix> + Ensure that <code>AsyncListener.onComplete()</code> is called and that + the count of in-progress asynchronous requests is decremented when + buffered output cannot be flushed as a dispatch that can no longer be + performed is completed, for example because the client has reset the + HTTP/2 stream. (markt) + </fix> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
