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 7ff1b550b70531f5fa8b50779bc124fb1a1562ef
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Dec 1 17:46:58 2026 +0000

    Read the action hook once when performing a request action
    
    Request.action() read the volatile hook field twice. The field is cleared 
when
    the request is recycled, which can happen on another thread while a
    non-container thread is still finishing (for example after calling
    AsyncContext.complete()), causing a NullPointerException. Use a local copy.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 java/org/apache/coyote/Request.java           |  5 +++
 test/org/apache/coyote/TestRequestAction.java | 65 +++++++++++++++++++++++++++
 webapps/docs/changelog.xml                    |  6 +++
 3 files changed, 76 insertions(+)

diff --git a/java/org/apache/coyote/Request.java 
b/java/org/apache/coyote/Request.java
index f53274c013..e40408eb34 100644
--- a/java/org/apache/coyote/Request.java
+++ b/java/org/apache/coyote/Request.java
@@ -763,6 +763,11 @@ public final class Request {
      * @param param the action parameter
      */
     public void action(ActionCode actionCode, Object param) {
+        /*
+         * Take a local copy. The hook is cleared when the request is recycled 
which can happen on another thread, for
+         * example when a non-container thread is still finishing after having 
called AsyncContext.complete().
+         */
+        ActionHook hook = this.hook;
         if (hook != null) {
             hook.action(actionCode, Objects.requireNonNullElse(param, this));
         }
diff --git a/test/org/apache/coyote/TestRequestAction.java 
b/test/org/apache/coyote/TestRequestAction.java
new file mode 100644
index 0000000000..aded37a157
--- /dev/null
+++ b/test/org/apache/coyote/TestRequestAction.java
@@ -0,0 +1,65 @@
+/*
+ * 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.concurrent.atomic.AtomicReference;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestRequestAction {
+
+    /*
+     * The hook is cleared when a request is recycled. That can happen on 
another thread while a non-container thread is
+     * still calling action(), for example after it has called 
AsyncContext.complete(). The call must not fail. The test
+     * is probabilistic: it fails quickly if action() reads the hook field 
more than once but always passes if it does
+     * not.
+     */
+    @Test
+    public void testActionWhileHookCleared() throws Exception {
+        final Request request = new Request();
+        final ActionHook hook = (actionCode, param) -> { };
+        final AtomicReference<Throwable> failure = new AtomicReference<>();
+        final long end = System.nanoTime() + 2_000_000_000L;
+
+        Thread toggler = new Thread(() -> {
+            while (System.nanoTime() < end && failure.get() == null) {
+                request.setHook(hook);
+                request.setHook(null);
+            }
+        });
+        Thread caller = new Thread(() -> {
+            try {
+                while (System.nanoTime() < end && failure.get() == null) {
+                    request.action(ActionCode.DISPATCH_EXECUTE, null);
+                }
+            } catch (Throwable t) {
+                failure.set(t);
+            }
+        });
+
+        toggler.start();
+        caller.start();
+        caller.join();
+        toggler.join();
+
+        if (failure.get() != null) {
+            throw new AssertionError("action() failed while the hook was being 
cleared", failure.get());
+        }
+        Assert.assertNull(failure.get());
+    }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 0a02c2da51..b7d1f879b9 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -434,6 +434,12 @@
         Only return an NIO2 channel to the cache of channels for re-use once
         the connection that was using it is idle. (markt)
       </fix>
+      <fix>
+        Avoid a <code>NullPointerException</code> when a non-container thread
+        is still using a request, for example after calling
+        <code>AsyncContext.complete()</code>, as the request is recycled.
+        (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to