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

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 6a1746c2d Fix #1989: fall back to stdout if SLF4J is unavailable when 
logging (#2266)
6a1746c2d is described below

commit 6a1746c2d149e3db3b2414ba9f6c9db05862d002
Author: Arturo Bernal <[email protected]>
AuthorDate: Wed Nov 18 16:01:29 2026 +0100

    Fix #1989: fall back to stdout if SLF4J is unavailable when logging (#2266)
    
    #1989 was fixed by #2157, but reopened: `sendAcquireResponse()`'s catch 
block calls `error()` without the `if (!closing)` guard that protects every 
other log call in this class, so it can still run after `IpcServer.run()`'s 
`finally` block has reset the static `forkedProcess` flag to `false` during 
shutdown. In the real forked server process `slf4j-api` is never on the 
classpath (it's a `provided` dependency, and `IpcClient` only puts this 
module's own jar on the forked classpath), so [...]
    
    Wrapped the three `LoggerFactory` calls in `debug()`/`info()`/`error()` 
with a catch for `LinkageError`, falling back to the same stdout format already 
used for the forked-process path.
    
    Added `IpcServerLoggingWithoutSlf4jTest`, which loads the real compiled 
`IpcServer` class through a classloader that mirrors the forked process 
classpath (`org.slf4j.*` unresolvable, `forkedProcess` at its default `false`) 
and confirms `error()` crashes with `NoClassDefFoundError` without this change, 
and falls back cleanly with it.
    
    Also ran the module's real integration tests (`-Prun-its`, which actually 
fork the server) to confirm nothing else regressed: all green.
    
    ---
    
    To make clear that you license your contribution under the Apache License 
Version 2.0, January 2004, check one of the following:
    
    - [x] I hereby declare this contribution to be licensed under the Apache 
License Version 2.0, January 2004
    
    - [x] I hereby declare that I have filed an Apache Individual Contributor 
License Agreement.
---
 .../org/eclipse/aether/named/ipc/IpcServer.java    |  28 +++-
 .../ipc/IpcServerLoggingWithoutSlf4jTest.java      | 152 +++++++++++++++++++++
 2 files changed, 177 insertions(+), 3 deletions(-)

diff --git 
a/maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcServer.java
 
b/maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcServer.java
index 603173966..8a0f6944c 100644
--- 
a/maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcServer.java
+++ 
b/maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcServer.java
@@ -221,7 +221,14 @@ public class IpcServer {
             if (forkedProcess) {
                 System.out.println("[ipc] [debug] " + format(msg, args));
             } else {
-                LoggerFactory.getLogger(IpcServer.class).debug(msg, args);
+                try {
+                    LoggerFactory.getLogger(IpcServer.class).debug(msg, args);
+                } catch (LinkageError e) {
+                    // forkedProcess was reset to false while this call was 
already in flight (run()'s
+                    // finally block races callers such as 
sendAcquireResponse() during shutdown); slf4j-api
+                    // is still absent from the forked process classpath, so 
fall back the same way.
+                    System.out.println("[ipc] [debug] " + format(msg, args));
+                }
             }
         }
     }
@@ -230,7 +237,14 @@ public class IpcServer {
         if (forkedProcess) {
             System.out.println("[ipc] [info] " + format(msg, args));
         } else {
-            LoggerFactory.getLogger(IpcServer.class).info(msg, args);
+            try {
+                LoggerFactory.getLogger(IpcServer.class).info(msg, args);
+            } catch (LinkageError e) {
+                // forkedProcess was reset to false while this call was 
already in flight (run()'s
+                // finally block races callers such as sendAcquireResponse() 
during shutdown); slf4j-api
+                // is still absent from the forked process classpath, so fall 
back the same way.
+                System.out.println("[ipc] [info] " + format(msg, args));
+            }
         }
     }
 
@@ -239,7 +253,15 @@ public class IpcServer {
             System.out.println("[ipc] [error] " + msg);
             t.printStackTrace(System.out);
         } else {
-            LoggerFactory.getLogger(IpcServer.class).error(msg, t);
+            try {
+                LoggerFactory.getLogger(IpcServer.class).error(msg, t);
+            } catch (LinkageError e) {
+                // forkedProcess was reset to false while this call was 
already in flight (run()'s
+                // finally block races callers such as sendAcquireResponse() 
during shutdown); slf4j-api
+                // is still absent from the forked process classpath, so fall 
back the same way.
+                System.out.println("[ipc] [error] " + msg);
+                t.printStackTrace(System.out);
+            }
         }
     }
 
diff --git 
a/maven-resolver-named-locks-ipc/src/test/java/org/eclipse/aether/named/ipc/IpcServerLoggingWithoutSlf4jTest.java
 
b/maven-resolver-named-locks-ipc/src/test/java/org/eclipse/aether/named/ipc/IpcServerLoggingWithoutSlf4jTest.java
new file mode 100644
index 000000000..bb6984a5e
--- /dev/null
+++ 
b/maven-resolver-named-locks-ipc/src/test/java/org/eclipse/aether/named/ipc/IpcServerLoggingWithoutSlf4jTest.java
@@ -0,0 +1,152 @@
+/*
+ * 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.eclipse.aether.named.ipc;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.security.CodeSource;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+/**
+ * IpcServer.error()/info()/debug() route to SLF4J whenever {@code 
forkedProcess} is {@code false}, but
+ * {@code forkedProcess} is a single static flag that {@link IpcServer#run()} 
resets to {@code false} in its
+ * {@code finally} block as soon as the accept loop exits, including during 
the same JVM's shutdown sequence
+ * where other code paths (e.g. a lock-acquire callback racing the server's 
own close()) still call these
+ * methods. In the real forked server process, slf4j-api is never on the 
classpath (it is a "provided"
+ * dependency, and {@link IpcClient}'s forked classpath only ever contains 
this module's own jar) so those
+ * calls crash with {@link NoClassDefFoundError} instead of falling back to 
the forked-process stdout path.
+ *
+ * <p>This loads the real, compiled {@code IpcServer} class through an 
isolated classloader that mirrors the
+ * forked process classpath exactly: slf4j classes are unresolvable, 
everything else resolves normally, and
+ * {@code forkedProcess} starts at its default value of {@code false} (as it 
is after a shutdown reset) since
+ * {@code main()} was never called on this fresh class instance.
+ */
+class IpcServerLoggingWithoutSlf4jTest {
+
+    private static final String IPC_PACKAGE = "org.eclipse.aether.named.ipc.";
+
+    private static Class<?> loadIpcServerWithoutSlf4j() throws 
ClassNotFoundException {
+        CodeSource codeSource = 
IpcServer.class.getProtectionDomain().getCodeSource();
+        URL classesUrl = codeSource.getLocation();
+        ClassLoader blockingParent = new 
ClassLoader(IpcServer.class.getClassLoader()) {
+            @Override
+            protected Class<?> loadClass(String name, boolean resolve) throws 
ClassNotFoundException {
+                if (name.startsWith("org.slf4j.")) {
+                    throw new ClassNotFoundException(name);
+                }
+                return super.loadClass(name, resolve);
+            }
+        };
+        ClassLoader isolated = new URLClassLoader(new URL[] {classesUrl}, 
blockingParent) {
+            @Override
+            protected Class<?> loadClass(String name, boolean resolve) throws 
ClassNotFoundException {
+                synchronized (getClassLoadingLock(name)) {
+                    Class<?> c = findLoadedClass(name);
+                    if (c == null) {
+                        if (name.startsWith("org.slf4j.")) {
+                            throw new ClassNotFoundException(name);
+                        }
+                        c = name.startsWith(IPC_PACKAGE) ? findClass(name) : 
super.loadClass(name, false);
+                    }
+                    if (resolve) {
+                        resolveClass(c);
+                    }
+                    return c;
+                }
+            }
+        };
+        return Class.forName(IpcServer.class.getName(), true, isolated);
+    }
+
+    private static Method errorMethod(Class<?> ipcServerClass) throws 
NoSuchMethodException {
+        Method method = ipcServerClass.getDeclaredMethod("error", 
String.class, Throwable.class);
+        method.setAccessible(true);
+        return method;
+    }
+
+    private static Method infoMethod(Class<?> ipcServerClass) throws 
NoSuchMethodException {
+        Method method = ipcServerClass.getDeclaredMethod("info", String.class, 
Object[].class);
+        method.setAccessible(true);
+        return method;
+    }
+
+    private static Method debugMethod(Class<?> ipcServerClass) throws 
NoSuchMethodException {
+        Method method = ipcServerClass.getDeclaredMethod("debug", 
String.class, Object[].class);
+        method.setAccessible(true);
+        return method;
+    }
+
+    @Test
+    void errorTakesTheForkedProcessStdoutPathDirectlyWithoutExercisingTheFix() 
throws Exception {
+        // with forkedProcess=true, error() takes the "if (forkedProcess)" 
branch straight to stdout and never
+        // reaches the new try/catch (LinkageError) block below; this is a 
sanity check for that pre-existing
+        // path, not a validation of the fix - see 
errorFallsBackToStdoutWhenForkedProcessFlagHasBeenReset...()
+        // for the test that actually exercises the catch block.
+        Class<?> ipcServerClass = loadIpcServerWithoutSlf4j();
+        Field forkedProcess = ipcServerClass.getDeclaredField("forkedProcess");
+        forkedProcess.setAccessible(true);
+        forkedProcess.setBoolean(null, true);
+
+        assertDoesNotThrow(() -> errorMethod(ipcServerClass).invoke(null, 
"sanity check", new IOException("boom")));
+    }
+
+    @Test
+    void 
errorFallsBackToStdoutWhenForkedProcessFlagHasBeenResetDuringShutdown() throws 
Exception {
+        // forkedProcess defaults to false on this freshly loaded class, 
exactly as it is in the real forked
+        // process once IpcServer.run()'s finally block has reset it while 
shutting down - the exact state a
+        // lock-acquire callback (IpcServer.sendAcquireResponse(), whose catch 
block calls error() without an
+        // "if (!closing)" guard, unlike every other call site in the class) 
can still observe.
+        Class<?> ipcServerClass = loadIpcServerWithoutSlf4j();
+
+        assertDoesNotThrow(
+                () -> errorMethod(ipcServerClass).invoke(null, "Error writing 
lock response", new IOException("boom")));
+    }
+
+    @Test
+    void 
infoFallsBackToStdoutWhenForkedProcessFlagHasBeenResetDuringShutdown() throws 
Exception {
+        // same scenario as the error() test above, for the identical catch 
(LinkageError) block added to
+        // info(); forkedProcess defaults to false on this freshly loaded 
class.
+        Class<?> ipcServerClass = loadIpcServerWithoutSlf4j();
+
+        assertDoesNotThrow(
+                () -> infoMethod(ipcServerClass).invoke(null, "New client 
connected ({} connected)", new Object[] {1}));
+    }
+
+    @Test
+    void 
debugFallsBackToStdoutWhenForkedProcessFlagHasBeenResetDuringShutdown() throws 
Exception {
+        // debug() is a no-op unless the aether.named.ipc.debug system 
property is set, since DEBUG is a static
+        // final field read once at class-load time; it must be set before 
loading the class below, or the
+        // catch (LinkageError) block under test is never reached. Same 
scenario as the error()/info() tests
+        // above otherwise: forkedProcess defaults to false on this freshly 
loaded class.
+        System.setProperty(IpcServer.SYSTEM_PROP_DEBUG, "true");
+        try {
+            Class<?> ipcServerClass = loadIpcServerWithoutSlf4j();
+
+            assertDoesNotThrow(() -> debugMethod(ipcServerClass).invoke(null, 
"Received request {}", new Object[] {1}));
+        } finally {
+            System.clearProperty(IpcServer.SYSTEM_PROP_DEBUG);
+        }
+    }
+}

Reply via email to