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 5906790d178e079445f1e08f925e06548fe12a94 Author: Mark Thomas <[email protected]> AuthorDate: Tue Dec 1 19:06:46 2026 +0000 Clear the response error state if a dispatch wins the race with error handling If the application called dispatch() between the final check in AsyncContextImpl.setErrorState() that the request was still in the error state and the container's call to complete(), the failure of complete() was ignored but the error state on the response was left in place. The dispatch then triggered error page handling rather than reaching its target. Clear the error state in that case, as is already done if the dispatch happens earlier. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- .../org/apache/catalina/core/AsyncContextImpl.java | 7 ++ .../coyote/TestAsyncContextImplSetErrorState.java | 95 ++++++++++++++++------ webapps/docs/changelog.xml | 6 ++ 3 files changed, 83 insertions(+), 25 deletions(-) diff --git a/java/org/apache/catalina/core/AsyncContextImpl.java b/java/org/apache/catalina/core/AsyncContextImpl.java index 407cfda0b8..b5e0087ae1 100644 --- a/java/org/apache/catalina/core/AsyncContextImpl.java +++ b/java/org/apache/catalina/core/AsyncContextImpl.java @@ -641,6 +641,13 @@ public class AsyncContextImpl implements AsyncContext, AsyncContextCallback { if (result.get()) { throw ise; } + if (request.isAsyncDispatching()) { + /* + * The application called dispatch(). As above, clear the error state on the response else + * the dispatch will trigger error page handling. + */ + request.getResponse().resetError(); + } } } } else if (request.isAsyncDispatching()) { diff --git a/test/org/apache/coyote/TestAsyncContextImplSetErrorState.java b/test/org/apache/coyote/TestAsyncContextImplSetErrorState.java index 0275da90c4..21497054ef 100644 --- a/test/org/apache/coyote/TestAsyncContextImplSetErrorState.java +++ b/test/org/apache/coyote/TestAsyncContextImplSetErrorState.java @@ -16,6 +16,7 @@ */ package org.apache.coyote; +import java.lang.reflect.Field; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -25,26 +26,34 @@ import org.junit.Test; import org.apache.catalina.core.AsyncContextImpl; /** - * Tests {@link AsyncContextImpl#setErrorState(Throwable, boolean)} when the application completes the asynchronous - * processing between the container checking that the request is still in the error state and the container completing - * it. The window is a few instructions wide so the competing call is simulated by an {@link ActionHook} that stands in - * for the async state machine. + * Tests {@link AsyncContextImpl#setErrorState(Throwable, boolean)} when the application completes or dispatches the + * asynchronous processing between the container checking that the request is still in the error state and the + * container completing it. The window is a few instructions wide so the competing call is simulated by an + * {@link ActionHook} that stands in for the async state machine. */ public class TestAsyncContextImplSetErrorState { + private enum Competitor { + NONE, + COMPLETE, + DISPATCH + } + + /** - * Mimics the parts of the async state machine used by setErrorState(): the error state, and a state that rejects a - * second call to complete(). + * Mimics the parts of the async state machine used by setErrorState(): the error state, the dispatching state, and + * a state that rejects a second call to complete(). */ private static class StubHook implements ActionHook { - private final boolean competingComplete; + private final Competitor competitor; private final AtomicBoolean inError = new AtomicBoolean(true); - private final AtomicBoolean completed = new AtomicBoolean(false); + private final AtomicBoolean dispatching = new AtomicBoolean(false); + private final AtomicBoolean finished = new AtomicBoolean(false); private final AtomicInteger isErrorChecks = new AtomicInteger(); - StubHook(boolean competingComplete) { - this.competingComplete = competingComplete; + StubHook(Competitor competitor) { + this.competitor = competitor; } @Override @@ -55,14 +64,18 @@ public class TestAsyncContextImplSetErrorState { // Checks 1 and 2 are the two made by setErrorState() before it calls complete() int check = isErrorChecks.incrementAndGet(); result.set(inError.get()); - if (competingComplete && check == 2) { - // The application calls complete() after the container has seen the error state - complete(); + if (check == 2 && competitor != Competitor.NONE) { + // The application calls complete() or dispatch() after the container has seen the error state + finish(competitor == Competitor.DISPATCH); } break; } + case ASYNC_IS_DISPATCHING: { + ((AtomicBoolean) param).set(dispatching.get()); + break; + } case ASYNC_COMPLETE: { - complete(); + finish(false); break; } default: @@ -70,41 +83,73 @@ public class TestAsyncContextImplSetErrorState { } } - private void complete() { - if (!completed.compareAndSet(false, true)) { + private void finish(boolean dispatch) { + if (!finished.compareAndSet(false, true)) { throw new IllegalStateException(); } inError.set(false); + dispatching.set(dispatch); } } - private static AsyncContextImpl createAsyncContext(StubHook hook) { + private static AsyncContextImpl createAsyncContext(StubHook hook, org.apache.coyote.Response coyoteResponse) + throws Exception { Request coyoteRequest = new Request(); coyoteRequest.setHook(hook); org.apache.catalina.connector.Request request = new org.apache.catalina.connector.Request(null); request.setCoyoteRequest(coyoteRequest); - return new AsyncContextImpl(request); + org.apache.catalina.connector.Response response = new org.apache.catalina.connector.Response(); + response.setCoyoteResponse(coyoteResponse); + request.setResponse(response); + AsyncContextImpl asyncContext = new AsyncContextImpl(request); + // Request.isAsyncDispatching() reports false unless the request has an async context + Field field = org.apache.catalina.connector.Request.class.getDeclaredField("asyncContext"); + field.setAccessible(true); + field.set(request, asyncContext); + return asyncContext; } @Test - public void testCompleteCalledByApplicationDuringErrorHandling() { - StubHook hook = new StubHook(true); - AsyncContextImpl asyncContext = createAsyncContext(hook); + public void testCompleteCalledByApplicationDuringErrorHandling() throws Exception { + StubHook hook = new StubHook(Competitor.COMPLETE); + org.apache.coyote.Response coyoteResponse = new org.apache.coyote.Response(); + coyoteResponse.setError(); + AsyncContextImpl asyncContext = createAsyncContext(hook, coyoteResponse); // The container's call to complete() loses the race and must not fail asyncContext.setErrorState(null, false); // The two checks before complete() plus the check after it failed Assert.assertEquals(3, hook.isErrorChecks.get()); - Assert.assertTrue(hook.completed.get()); + Assert.assertTrue(hook.finished.get()); + // The request completes with the error that was reported + Assert.assertTrue(coyoteResponse.isErrorReportRequired()); + } + + + @Test + public void testDispatchCalledByApplicationDuringErrorHandling() throws Exception { + StubHook hook = new StubHook(Competitor.DISPATCH); + org.apache.coyote.Response coyoteResponse = new org.apache.coyote.Response(); + coyoteResponse.setError(); + AsyncContextImpl asyncContext = createAsyncContext(hook, coyoteResponse); + + // The container's call to complete() loses the race and must not fail + asyncContext.setErrorState(null, false); + + Assert.assertEquals(3, hook.isErrorChecks.get()); + Assert.assertTrue(hook.finished.get()); + // The error state must be cleared else the dispatch triggers error page handling instead of reaching the target + Assert.assertFalse(coyoteResponse.isError()); + Assert.assertFalse(coyoteResponse.isErrorReportRequired()); } @Test - public void testCompleteFailureWhileStillInErrorStateIsReported() { - StubHook hook = new StubHook(false) { + public void testCompleteFailureWhileStillInErrorStateIsReported() throws Exception { + StubHook hook = new StubHook(Competitor.NONE) { @Override public void action(ActionCode actionCode, Object param) { if (actionCode == ActionCode.ASYNC_COMPLETE) { @@ -114,7 +159,7 @@ public class TestAsyncContextImplSetErrorState { super.action(actionCode, param); } }; - AsyncContextImpl asyncContext = createAsyncContext(hook); + AsyncContextImpl asyncContext = createAsyncContext(hook, new org.apache.coyote.Response()); try { asyncContext.setErrorState(null, false); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 8f5bf1ff34..e0e2ae868e 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -233,6 +233,12 @@ application completes an asynchronous request while the container is completing it after an error. (markt) </fix> + <fix> + Ensure that an application call to + <code>AsyncContext.dispatch()</code> that races with the container's + handling of an asynchronous error is dispatched to its target rather + than being treated as a request in error. (markt) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
