gnodet-bot commented on code in PR #2157:
URL: https://github.com/apache/maven-resolver/pull/2157#discussion_r4082641892


##########
maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcServer.java:
##########
@@ -39,13 +39,17 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.slf4j.LoggerFactory;
+
 /**
  * Implementation of the server side.
  * The server instance is bound to a given maven repository.
  *
  * @since 2.0.1
  */
 public class IpcServer {
+    private static volatile boolean forkedProcess;

Review Comment:
   ⚠️ **`static volatile boolean forkedProcess` is permanent — no reset path.**
   
   Once `main()` sets `forkedProcess = true`, it stays `true` for the lifetime 
of the JVM. In normal operation (forked subprocess) this is fine because that 
JVM exits. But if a future test ever calls `IpcServer.main()` in-process (e.g. 
to test the `main()` entry point directly), all subsequent in-process uses of 
`IpcServer` will silently use `System.out` instead of SLF4J — exactly the 
regression this PR is fixing.
   
   The field should be package-private and reset when the server shuts down, or 
scoped so it doesn't bleed across test cases:
   
   ```suggestion
       static volatile boolean forkedProcess;
   ```
   
   (Package-private lets tests reset it with `IpcServer.forkedProcess = false` 
in `@AfterEach` if needed, without requiring a public API.)



##########
maven-resolver-named-locks-ipc/src/test/java/org/eclipse/aether/named/ipc/IpcClientForkedTest.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Duration;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class IpcClientForkedTest {
+
+    @Test
+    @Timeout(10)
+    void forkedServerDiagnosticsAreWrittenToLog(@TempDir Path tempDir) throws 
Exception {
+        Path logPath = tempDir.resolve("log");
+        IpcClient client = new IpcClient(tempDir.resolve("repository"), 
logPath, null);
+        try {
+            client.ensureInitialized();
+
+            long deadline = System.nanoTime() + 
Duration.ofSeconds(5).toNanos();
+            String logOutput = "";
+            while (System.nanoTime() < deadline) {
+                List<Path> logFiles;
+                try (var paths = Files.list(logPath)) {
+                    logFiles = paths.toList();
+                }
+                if (!logFiles.isEmpty()) {
+                    logOutput = Files.readString(logFiles.get(0), 
StandardCharsets.UTF_8);
+                    if (logOutput.contains("IpcServer started at")) {
+                        break;
+                    }
+                }
+                Thread.sleep(10);
+            }
+
+            assertTrue(
+                    logOutput.contains("IpcServer started at"),
+                    "expected forked server startup diagnostics in log, but 
was: " + logOutput);
+        } finally {
+            if (client.initialized) {
+                client.stopServer();
+            }
+            client.close();

Review Comment:
   ⚠️ **Resource leak if `stopServer()` throws.**
   
   `stopServer()` wraps its failure in a `RuntimeException` and rethrows (see 
`IpcClient.java:469`). If it throws here, `client.close()` at line 66 is 
unreachable — the forked server process is never killed. In CI this leaks a 
lingering subprocess that may hold the socket address, causing subsequent test 
runs to fail.
   
   Fix with a nested try:
   
   ```suggestion
           } finally {
               try {
                   if (client.initialized) {
                       client.stopServer();
                   }
               } finally {
                   client.close();
               }
   ```



-- 
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]

Reply via email to