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

rzo1 pushed a commit to branch TOMEE-4703
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit 340376ed254b0cd95e0f00bf9527eeff6c9c8134
Author: Richard Zowalla <[email protected]>
AuthorDate: Wed Sep 9 20:46:51 2026 +0200

    TOMEE-4703 restore the caller's class loader after exiting the ThreadContext
    
    ApplicationThreadContextRestorer.endContext() restored the class loader it 
had
    saved and then called ThreadContext.exit(), which sets the loader again 
from the
    value the context recorded on entry. That value is the application class 
loader
    installed by begin(), so the thread was left with the application class 
loader
    instead of the one it arrived with.
    
    Exit first and restore the loader afterwards. This affects pool threads, 
where
    the captured context is never the thread's current context and the identity
    guard in begin() therefore never applies.
---
 .../threads/impl/ApplicationThreadContextProvider.java        | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git 
a/container/openejb-core/src/main/java/org/apache/openejb/threads/impl/ApplicationThreadContextProvider.java
 
b/container/openejb-core/src/main/java/org/apache/openejb/threads/impl/ApplicationThreadContextProvider.java
index 5977812e3b..ac925f58da 100755
--- 
a/container/openejb-core/src/main/java/org/apache/openejb/threads/impl/ApplicationThreadContextProvider.java
+++ 
b/container/openejb-core/src/main/java/org/apache/openejb/threads/impl/ApplicationThreadContextProvider.java
@@ -100,13 +100,16 @@ public class ApplicationThreadContextProvider implements 
ThreadContextProvider,
 
         @Override
         public void endContext() throws IllegalStateException {
-            if (oldClassLoader != null) {
-                Thread.currentThread().setContextClassLoader(oldClassLoader);
-            }
-
+            // exit before restoring the class loader. ThreadContext.exit sets 
the loader to the value
+            // the context recorded on entry, which is the application class 
loader installed by
+            // begin(), so restoring afterwards leaves the thread with the 
loader it started with.
             if (exitThreadContext) {
                 ThreadContext.exit(oldThreadContext);
             }
+
+            if (oldClassLoader != null) {
+                Thread.currentThread().setContextClassLoader(oldClassLoader);
+            }
         }
 
         @Override

Reply via email to