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 212caf542dc5bea9e4c11007814d9a73ac9f730b Author: Mark Thomas <[email protected]> AuthorDate: Tue Dec 1 17:44:57 2026 +0000 Complete async processing even if the buffered output cannot be flushed If flushing buffered output failed when asynchronous processing 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. The identical handling in the three affected states is moved to a single method. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- java/org/apache/coyote/AsyncStateMachine.java | 50 ++++++----- .../coyote/TestAsyncStateMachineFlushFailure.java | 99 ++++++++++++++++++++++ webapps/docs/changelog.xml | 6 ++ 3 files changed, 134 insertions(+), 21 deletions(-) diff --git a/java/org/apache/coyote/AsyncStateMachine.java b/java/org/apache/coyote/AsyncStateMachine.java index c77d3f9cc3..e01634ff30 100644 --- a/java/org/apache/coyote/AsyncStateMachine.java +++ b/java/org/apache/coyote/AsyncStateMachine.java @@ -295,13 +295,7 @@ class AsyncStateMachine { updateState(AsyncState.STARTED); return SocketState.LONG; } else if (state == AsyncState.MUST_COMPLETE || state == AsyncState.COMPLETING) { - if (processor.getErrorState().isIoAllowed() && processor.flushBufferedWrite()) { - return SocketState.LONG; - } - asyncCtxt.fireOnComplete(); - updateState(AsyncState.DISPATCHED); - asyncCtxt.decrementInProgressAsyncCount(); - return SocketState.ASYNC_END; + return completeProcessing(); } else if (state == AsyncState.MUST_DISPATCH) { if (!processor.getErrorState().isError()) { updateState(AsyncState.DISPATCHING); @@ -317,21 +311,9 @@ 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; - } - asyncCtxt.fireOnComplete(); - updateState(AsyncState.DISPATCHED); - asyncCtxt.decrementInProgressAsyncCount(); - return SocketState.ASYNC_END; + return completeProcessing(); } else if (state == AsyncState.DISPATCHING) { - if (processor.getErrorState().isIoAllowed() && processor.flushBufferedWrite()) { - return SocketState.LONG; - } - asyncCtxt.fireOnComplete(); - updateState(AsyncState.DISPATCHED); - asyncCtxt.decrementInProgressAsyncCount(); - return SocketState.ASYNC_END; + return completeProcessing(); } else if (state == AsyncState.STARTED) { // This can occur if an async listener does a dispatch to an async // servlet during onTimeout @@ -386,6 +368,32 @@ class AsyncStateMachine { } + /* + * Completes the processing of a request that is completing or that can no longer be dispatched. Any buffered output + * is flushed first. If that output can't be written yet, wait for the write to finish. 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. + */ + private SocketState completeProcessing() throws IOException { + 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; + } + + synchronized boolean asyncTimeout() { if (state == AsyncState.STARTED) { updateState(AsyncState.TIMING_OUT); diff --git a/test/org/apache/coyote/TestAsyncStateMachineFlushFailure.java b/test/org/apache/coyote/TestAsyncStateMachineFlushFailure.java new file mode 100644 index 0000000000..a5320ce2e2 --- /dev/null +++ b/test/org/apache/coyote/TestAsyncStateMachineFlushFailure.java @@ -0,0 +1,99 @@ +/* + * 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 asynchronous processing completes, for example because the client + * has reset the stream, does not prevent the request from being completed. + */ +public class TestAsyncStateMachineFlushFailure { + + @Test + public void testFlushFailureWhenCompleting() { + 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); + + // 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 931eac6040..c8c962b176 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -440,6 +440,12 @@ <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 asynchronous processing completes, + 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]
