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

rmaucher pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new c54c81d4ec Generalize null checks in AsyncContext
c54c81d4ec is described below

commit c54c81d4ec53c9d47af5ff5257df65e0af6519f8
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 ffef8d30ad..7ce427b993 100644
--- a/java/org/apache/catalina/core/AsyncContextImpl.java
+++ b/java/org/apache/catalina/core/AsyncContextImpl.java
@@ -110,8 +110,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 {
@@ -122,9 +126,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);
+            }
         }
     }
 
@@ -144,7 +152,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) {
@@ -157,7 +168,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();
@@ -180,7 +193,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);
@@ -274,7 +288,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) {
@@ -478,10 +497,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);
@@ -502,13 +524,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