This is an automated email from the ASF dual-hosted git repository.

rmaucher 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 09c3806ed2 Generalize null checks in AsyncContext
09c3806ed2 is described below

commit 09c3806ed2d1a3a0455b87cdd3fabecfefc5e6a3
Author: remm <[email protected]>
AuthorDate: Wed May 20 15:44:17 2026 +0200

    Generalize null checks in AsyncContext
---
 .../org/apache/catalina/core/AsyncContextImpl.java | 54 ++++++++++++++++------
 1 file changed, 41 insertions(+), 13 deletions(-)

diff --git a/java/org/apache/catalina/core/AsyncContextImpl.java 
b/java/org/apache/catalina/core/AsyncContextImpl.java
index e070129a34..e22dffba67 100644
--- a/java/org/apache/catalina/core/AsyncContextImpl.java
+++ b/java/org/apache/catalina/core/AsyncContextImpl.java
@@ -111,8 +111,12 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
             log.trace(sm.getString("asyncContextImpl.fireOnComplete"));
         }
         List<AsyncListenerWrapper> listenersCopy = new ArrayList<>(listeners);
+        Context context = this.context;
 
-        ClassLoader oldCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
+        ClassLoader oldCL = null;
+        if (context != null) {
+            oldCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
+        }
         try {
             for (AsyncListenerWrapper listener : listenersCopy) {
                 try {
@@ -123,9 +127,13 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
                 }
             }
         } finally {
-            context.fireRequestDestroyEvent(request.getRequest());
+            if (context != null) {
+                context.fireRequestDestroyEvent(request.getRequest());
+            }
             clearServletRequestResponse();
-            context.unbind(Globals.IS_SECURITY_ENABLED, oldCL);
+            if (context != null) {
+                context.unbind(Globals.IS_SECURITY_ENABLED, oldCL);
+            }
         }
     }
 
@@ -145,7 +153,10 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
             if (log.isTraceEnabled()) {
                 log.trace(sm.getString("asyncContextImpl.fireOnTimeout"));
             }
-            ClassLoader oldCL = context.bind(false, null);
+            ClassLoader oldCL = null;
+            if (context != null) {
+                oldCL = context.bind(false, null);
+            }
             try {
                 List<AsyncListenerWrapper> listenersCopy = new 
ArrayList<>(listeners);
                 for (AsyncListenerWrapper listener : listenersCopy) {
@@ -158,7 +169,9 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
                 }
                 
request.getCoyoteRequest().action(ActionCode.ASYNC_IS_TIMINGOUT, result);
             } finally {
-                context.unbind(false, oldCL);
+                if (context != null) {
+                    context.unbind(false, oldCL);
+                }
             }
         }
         return !result.get();
@@ -181,7 +194,8 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
         if (cpath.length() > 1) {
             path = path.substring(cpath.length());
         }
-        if (!context.getDispatchersUseEncodedPaths()) {
+        Context context = this.context;
+        if (context == null || context.getDispatchersUseEncodedPaths()) {
             path = UDecoder.URLDecode(path, StandardCharsets.UTF_8);
         }
         dispatch(path);
@@ -275,7 +289,12 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
         check();
         T listener;
         try {
-            listener = (T) 
context.getInstanceManager().newInstance(clazz.getName(), 
clazz.getClassLoader());
+            Context context = this.context;
+            if (context != null) {
+                listener = (T) 
context.getInstanceManager().newInstance(clazz.getName(), 
clazz.getClassLoader());
+            } else {
+                listener = (T) Class.forName(clazz.getName(), true, 
clazz.getClassLoader()).getConstructor().newInstance();
+            }
         } catch (ReflectiveOperationException | NamingException e) {
             throw new ServletException(e);
         } catch (Exception e) {
@@ -479,10 +498,13 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
                 ((HttpServletResponse) 
servletResponse).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
             }
 
-            Host host = (Host) context.getParent();
-            Valve stdHostValve = host.getPipeline().getBasic();
-            if (stdHostValve instanceof StandardHostValve) {
-                ((StandardHostValve) stdHostValve).throwable(request, 
request.getResponse(), t);
+            Context context = this.context;
+            if (context != null) {
+                Host host = (Host) context.getParent();
+                Valve stdHostValve = host.getPipeline().getBasic();
+                if (stdHostValve instanceof StandardHostValve) {
+                    ((StandardHostValve) stdHostValve).throwable(request, 
request.getResponse(), t);
+                }
             }
 
             request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, 
result);
@@ -503,13 +525,19 @@ public class AsyncContextImpl implements AsyncContext, 
AsyncContextCallback {
 
     @Override
     public void incrementInProgressAsyncCount() {
-        context.incrementInProgressAsyncCount();
+        Context context = this.context;
+        if (context != null) {
+            context.incrementInProgressAsyncCount();
+        }
     }
 
 
     @Override
     public void decrementInProgressAsyncCount() {
-        context.decrementInProgressAsyncCount();
+        Context context = this.context;
+        if (context != null) {
+            context.decrementInProgressAsyncCount();
+        }
     }
 
 


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

Reply via email to