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
The following commit(s) were added to refs/heads/10.1.x by this push:
new 948331cc4c Do not forward an asynchronous request in error to the
application again
948331cc4c is described below
commit 948331cc4cfd0eaacbaf477db423d2fd4d2ebd25
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Dec 1 10:42:33 2026 +0000
Do not forward an asynchronous request in error to the application again
If another thread reported the error for an asynchronous request between the
check in CoyoteAdapter that the error needs to be reported and the check in
StandardHostValve, StandardHostValve treated the request as not being in
error
and invoked the application a second time. Only forward a request that is in
error to the application if it is an asynchronous dispatch.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
.../apache/catalina/core/StandardHostValve.java | 7 +-
.../catalina/core/TestStandardHostValve.java | 136 +++++++++++++++++++++
webapps/docs/changelog.xml | 6 +
3 files changed, 147 insertions(+), 2 deletions(-)
diff --git a/java/org/apache/catalina/core/StandardHostValve.java
b/java/org/apache/catalina/core/StandardHostValve.java
index 4dece1739f..07aef06647 100644
--- a/java/org/apache/catalina/core/StandardHostValve.java
+++ b/java/org/apache/catalina/core/StandardHostValve.java
@@ -109,9 +109,12 @@ final class StandardHostValve extends ValveBase {
// Ask this Context to process this request. Requests that are
// already in error must have been routed here to check for
// application defined error pages so DO NOT forward them to the
- // application for processing.
+ // application for processing. If the error has already been
+ // reported (possibly by another thread) the request is still in
+ // error, so only forward it to the application if it is an async
+ // dispatch.
try {
- if (!response.isErrorReportRequired()) {
+ if (!response.isErrorReportRequired() && (!response.isError()
|| request.isAsyncDispatching())) {
context.getPipeline().getFirst().invoke(request, response);
}
} catch (Throwable t) {
diff --git a/test/org/apache/catalina/core/TestStandardHostValve.java
b/test/org/apache/catalina/core/TestStandardHostValve.java
index 48b4c847d7..b64d1f7551 100644
--- a/test/org/apache/catalina/core/TestStandardHostValve.java
+++ b/test/org/apache/catalina/core/TestStandardHostValve.java
@@ -20,7 +20,10 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import jakarta.servlet.AsyncContext;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequestEvent;
@@ -33,9 +36,12 @@ import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
+import org.apache.catalina.Wrapper;
+import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.catalina.valves.ValveBase;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.descriptor.web.ErrorPage;
@@ -232,6 +238,136 @@ public class TestStandardHostValve extends TomcatBaseTest
{
}
+ /*
+ * If the response is in error and that error has not yet been reported,
an asynchronous dispatch of the request
+ * must be handled as an error. It must not be forwarded to the dispatch
target.
+ */
+ @Test
+ public void testAsyncDispatchWithUnreportedError() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+
+ // No file system docBase required
+ Context ctx = getProgrammaticRootContext();
+
+ Wrapper start = Tomcat.addServlet(ctx, "start", new
ErrorThenDispatchServlet());
+ start.setAsyncSupported(true);
+ ctx.addServletMapping("/start", "start");
+
+ DispatchTargetServlet target = new DispatchTargetServlet();
+ Tomcat.addServlet(ctx, "target", target);
+ ctx.addServletMapping("/target", "target");
+
+ tomcat.start();
+
+ ByteChunk bc = new ByteChunk();
+ int rc = getUrl("http://localhost:" + getPort() + "/start", bc, null);
+
+ Assert.assertEquals(Response.SC_INTERNAL_SERVER_ERROR, rc);
+ Assert.assertFalse("The dispatch target was invoked for a request in
error", target.invoked.get());
+ }
+
+
+ private static class ErrorThenDispatchServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
+ AsyncContext ac = req.startAsync();
+ // Puts the response in error. The error has not been reported.
+ resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ ac.dispatch("/target");
+ }
+ }
+
+
+ private static class DispatchTargetServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private final transient AtomicBoolean invoked = new
AtomicBoolean(false);
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
+ invoked.set(true);
+ resp.setContentType("text/plain");
+ resp.getWriter().print("TARGET");
+ }
+ }
+
+
+ /*
+ * The error handling for an asynchronous request that times out checks
that the error needs to be reported and
+ * then passes the request to the container's pipeline for the error to be
reported. If another thread reports
+ * the error between that check and StandardHostValve, the request is
still in error and must not be forwarded to
+ * the application. Simulate the other thread with a valve so that the
test is deterministic.
+ */
+ @Test
+ public void testErrorReportedByAnotherThreadDuringAsyncErrorHandling()
throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+
+ // No file system docBase required
+ Context ctx = getProgrammaticRootContext();
+
+ CountingAsyncServlet servlet = new CountingAsyncServlet();
+ Wrapper wrapper = Tomcat.addServlet(ctx, "async", servlet);
+ wrapper.setAsyncSupported(true);
+ ctx.addServletMapping("/async", "async");
+
+ ErrorReportedByOtherThreadValve valve = new
ErrorReportedByOtherThreadValve();
+ tomcat.getHost().getPipeline().addValve(valve);
+
+ tomcat.start();
+
+ ByteChunk bc = new ByteChunk();
+ getUrl("http://localhost:" + getPort() + "/async", bc, null);
+
+ Assert.assertTrue("The test did not reach the point where the error is
reported by another thread",
+ valve.simulated.get());
+ Assert.assertEquals("The servlet was invoked again for a request in
error", 1, servlet.invocations.get());
+ }
+
+
+ private static class CountingAsyncServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private final transient AtomicInteger invocations = new
AtomicInteger(0);
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
+ if (invocations.incrementAndGet() == 1) {
+ AsyncContext ac = req.startAsync();
+ // No listener so the container's error handling completes the
request when the timeout occurs
+ ac.setTimeout(200);
+ // Puts the response in error. The error has not been reported.
+ resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ }
+ }
+ }
+
+
+ private static class ErrorReportedByOtherThreadValve extends ValveBase {
+
+ private final AtomicBoolean simulated = new AtomicBoolean(false);
+
+ ErrorReportedByOtherThreadValve() {
+ super(true);
+ }
+
+ @Override
+ public void invoke(Request request, Response response) throws
IOException, ServletException {
+ if (request.isAsync() && !request.isAsyncDispatching() &&
response.isErrorReportRequired() &&
+ simulated.compareAndSet(false, true)) {
+ // This is the container passing an asynchronous request in
error to the pipeline so the error can
+ // be reported. Simulate another thread reporting the error
first.
+ response.setErrorReported();
+ }
+ getNext().invoke(request, response);
+ }
+ }
+
+
private static class ErrorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index fb82b34277..0158312690 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -221,6 +221,12 @@
is no longer treated as a request for the file with the same name
without the trailing <code>/</code>. (remm)
</fix>
+ <fix>
+ Do not pass an asynchronous request to the application again when the
+ error that the request is in has already been reported, for example
+ when the container's error handling and the application complete the
+ request concurrently. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]