gnodet-bot commented on code in PR #2266: URL: https://github.com/apache/maven-resolver/pull/2266#discussion_r4485241132
########## maven-resolver-named-locks-ipc/src/test/java/org/eclipse/aether/named/ipc/IpcServerLoggingWithoutSlf4jTest.java: ########## @@ -0,0 +1,110 @@ +/* + * 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 { Review Comment: 💡 `errorMethod()` only gives `error()` coverage. `info()` and `debug()` received identical `catch (LinkageError)` fixes but are untested. Consider adding helpers and a third test that exercises the reset-flag path for one of them. ########## maven-resolver-named-locks-ipc/src/test/java/org/eclipse/aether/named/ipc/IpcServerLoggingWithoutSlf4jTest.java: ########## @@ -0,0 +1,110 @@ +/* + * 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; + } + + @Test + void errorFallsBackToStdoutWhenForkedProcessFlagIsCorrectlySet() throws Exception { Review Comment: ℹ️ This test sets `forkedProcess=true`, so `error()` takes the `if`-branch (direct stdout) and never reaches the new `try/catch (LinkageError)` block. It is a sanity check for the existing forked-process path, not a validation of the new catch. The second test (`errorFallsBackToStdoutWhenForkedProcessFlagHasBeenResetDuringShutdown`) is what actually validates the fix. Consider renaming or adding a comment that makes this role explicit. ########## maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcServer.java: ########## @@ -230,7 +237,11 @@ private static void info(String msg, Object... args) { 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) { Review Comment: 💡 The `debug()` catch block has a 3-line comment explaining why catching `LinkageError` is correct here (the `forkedProcess` flag race). The `info()` catch is bare — future maintainers reading this method won't see any explanation. Apply the same comment here (and in `error()`). -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
