You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "kalayciburak (via GitHub)" <gi...@apache.org> on 2026/09/08 10:34:41 UTC

[PR] [SUREFIRE-3460] Don't NPE when StackWalkerStrategy inits with a null TCCL [maven-surefire]

kalayciburak opened a new pull request, #3464:
URL: https://github.com/apache/maven-surefire/pull/3464

   `StackWalkerStrategy.<clinit>` loaded `java.lang.StackWalker` through the current thread's TCCL. When that loader is `null` (user threads that cleared it, or the JVM `Attach Listener` while a dynamic agent is installed), `ReflectionUtils.tryLoadClass` threw an uncaught `NullPointerException`. That failed class initialization and every later reference became `NoClassDefFoundError: Could not initialize class ...StackWalkerStrategy`.
   
   Because console capture walks the stack on every `println`, a write from a null-TCCL thread could poison the forked JVM. On 3.6.0 this also surfaces as a confusing Byte Buddy / BlockHound self-attach failure when the JDK prints the dynamic-agent warning from `Attach Listener`.
   
   This change:
   
   - treats a null `ClassLoader` as "not found" in `tryLoadClass`, and also swallows `RuntimeException` so a later NPE cannot poison `<clinit>`
   - resolves `StackWalker` / `StackFrame` from `StackWalkerStrategy`'s defining loader (system loader if that is bootstrap `null`), since those types are JDK classes and do not need the TCCL
   
   Fixes #3460
   
   Following this checklist to help us incorporate your contribution quickly and easily:
   
    - [x] Each commit in the pull request should have a meaningful subject line and body.
    - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [x] Run `mvn clean install` to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
    - [ ] You have run the integration tests successfully (`mvn -Prun-its clean install`).
   
   Validation (JDK 21, Maven 3.9.11):
   
    - `mvn -pl surefire-api -Dtest=ReflectionUtilsTest,StackTraceProviderTest test` — 23 tests, 0 failures
    - `mvn -pl surefire-api test` — 217 tests, 0 failures, 6 skipped
   
   If your pull request is about ~20 lines of code you don't need to sign an
   [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
   please ask on the developers list.
   
   To make clear that you license your contribution under
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
    - [x] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [ ] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SUREFIRE-3460] Don't NPE when StackWalkerStrategy inits with a null TCCL [maven-surefire]

Posted by "olamy (via GitHub)" <gi...@apache.org>.
olamy merged PR #3464:
URL: https://github.com/apache/maven-surefire/pull/3464


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SUREFIRE-3460] Don't NPE when StackWalkerStrategy inits with a null TCCL [maven-surefire]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #3464:
URL: https://github.com/apache/maven-surefire/pull/3464#discussion_r3967723594


##########
surefire-api/src/main/java/org/apache/maven/surefire/api/report/StackWalkerStrategy.java:
##########
@@ -55,7 +55,13 @@ final class StackWalkerStrategy {
     private static final Method FRAME_METHOD_NAME; // StackWalker.StackFrame.getMethodName() -> String
 
     static {
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        // java.lang.StackWalker is a JDK class; do not depend on the current thread's TCCL, which can be null
+        // (Attach Listener, user threads that cleared it). A null TCCL used to NPE in tryLoadClass and poison

Review Comment:
   only describe current code, not what uied to happen



##########
surefire-api/src/main/java/org/apache/maven/surefire/api/util/ReflectionUtils.java:
##########
@@ -148,9 +148,12 @@ public static <T> T instantiateObject(String className, Class<?>[] types, Object
 
     @SuppressWarnings("checkstyle:emptyblock")
     public static Class<?> tryLoadClass(ClassLoader classLoader, String className) {
+        if (classLoader == null) {
+            return null;
+        }
         try {
             return classLoader.loadClass(className);
-        } catch (NoClassDefFoundError | ClassNotFoundException ignore) {
+        } catch (NoClassDefFoundError | ClassNotFoundException | RuntimeException ignore) {

Review Comment:
   return null should probably move into this catch block
   
   Don't catch RuntimeException. Only catch more specific exceptions you actually expect. Otherwise we'll hide bugs we should know about



##########
surefire-api/src/main/java/org/apache/maven/surefire/api/report/StackWalkerStrategy.java:
##########
@@ -55,7 +55,13 @@ final class StackWalkerStrategy {
     private static final Method FRAME_METHOD_NAME; // StackWalker.StackFrame.getMethodName() -> String
 
     static {
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        // java.lang.StackWalker is a JDK class; do not depend on the current thread's TCCL, which can be null
+        // (Attach Listener, user threads that cleared it). A null TCCL used to NPE in tryLoadClass and poison

Review Comment:
   only describe current code, not what used to happen



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SUREFIRE-3460] Don't NPE when StackWalkerStrategy inits with a null TCCL [maven-surefire]

Posted by "kalayciburak (via GitHub)" <gi...@apache.org>.
kalayciburak commented on code in PR #3464:
URL: https://github.com/apache/maven-surefire/pull/3464#discussion_r3975777483


##########
surefire-api/src/test/java/org/apache/maven/surefire/api/report/StackTraceProviderTest.java:
##########
@@ -142,4 +142,18 @@ void strategyWalkAppliesExcludePredicate() {
         assertThat(frames).noneMatch(frame -> frame.startsWith(StackWalkerStrategy.class.getName() + "#"));
         assertThat(frames.get(0)).isEqualTo(getClass().getName() + "#strategyWalkAppliesExcludePredicate");
     }
+
+    @Test
+    void getStackDoesNotThrowWhenContextClassLoaderIsNull() {
+        ClassLoader original = Thread.currentThread().getContextClassLoader();

Review Comment:
   added a tiny IT, same repro as on the issue (null TCCL thread + one println). pushed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SUREFIRE-3460] Don't NPE when StackWalkerStrategy inits with a null TCCL [maven-surefire]

Posted by "olamy (via GitHub)" <gi...@apache.org>.
olamy commented on code in PR #3464:
URL: https://github.com/apache/maven-surefire/pull/3464#discussion_r3975003461


##########
surefire-api/src/test/java/org/apache/maven/surefire/api/report/StackTraceProviderTest.java:
##########
@@ -142,4 +142,18 @@ void strategyWalkAppliesExcludePredicate() {
         assertThat(frames).noneMatch(frame -> frame.startsWith(StackWalkerStrategy.class.getName() + "#"));
         assertThat(frames.get(0)).isEqualTo(getClass().getName() + "#strategyWalkAppliesExcludePredicate");
     }
+
+    @Test
+    void getStackDoesNotThrowWhenContextClassLoaderIsNull() {
+        ClassLoader original = Thread.currentThread().getContextClassLoader();

Review Comment:
   Thanks for your pull request.
   Could you please provide an IT test as well. A real as simple as possible project reproducing the issue



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org