gnodet commented on code in PR #1944:
URL: https://github.com/apache/maven-mvnd/pull/1944#discussion_r4792842310


##########
common/src/main/java/org/mvndaemon/mvnd/common/logging/TerminalOutput.java:
##########
@@ -157,6 +161,8 @@ public TerminalOutput(boolean noBuffering, int 
rollingWindowSize, Path logFile)
         builder.systemOutput(TerminalBuilder.SystemOutput.SysErr);
         // fall back to a dumb terminal silently when there is no system 
terminal, e.g. on headless CI
         builder.dumb(true);
+        // Ctrl+C is read as a key by TerminalInputHandler, so JLine must not 
also raise INT for it in raw mode

Review Comment:
   **Global JVM side effect**
   
   Setting this system property affects the entire JVM, not just this 
`TerminalOutput` instance. This could have unintended consequences if:
   - Multiple `TerminalOutput` instances are created with different requirements
   - Other code in the JVM expects software signals to be enabled
   - Tests run in parallel in the same JVM
   
   **Suggestions:**
   1. Document this side effect clearly in a comment explaining why it's 
necessary
   2. Consider checking if the property is already set before overwriting it
   3. Add a note in the class javadoc about this global configuration 
requirement
   
   Example:
   ```java
   // IMPORTANT: Disables JLine's software signal handling globally for this 
JVM.
   // We handle Ctrl+C manually in TerminalInputHandler to support cancellation
   // before the client connects to a daemon.
   System.setProperty(TerminalBuilder.PROP_SOFTWARE_SIGNALS, "false");
   ```
   
   _Claude Code on behalf of Guillaume Nodet_



##########
common/src/main/java/org/mvndaemon/mvnd/common/logging/TerminalInputHandler.java:
##########
@@ -170,11 +175,14 @@ private void handleProjectInput(String projectId, int 
bytesToRead) throws IOExce
     }
 
     private void handleControlKeys() throws IOException {
-        if (daemonReceive == null) {
+        if (daemonDispatch == null) {
             return;
         }
+        // Keep reading before the client is connected, so that Ctrl+C is 
seen; other keys are dropped until then
         int c = terminal.reader().read(10);
-        if (c != -1 && isControlKey(c)) {
+        if (c == TerminalOutput.KEY_CTRL_C) {

Review Comment:
   **Potential race condition on volatile field**
   
   While `daemonDispatch` is declared volatile (line 64), there's still a 
theoretical race condition here: the field could be set to null between the 
check on line 183 and the usage on line 185.
   
   In practice, this shouldn't happen since `daemonDispatch` is set once during 
initialization and never cleared. However, for defensive programming:
   
   **Suggestions:**
   1. Add a comment explaining that `daemonDispatch` is never set to null after 
initialization
   2. Consider storing the value in a local variable after the null check:
   ```java
   Consumer<Message> dispatch = daemonDispatch;
   if (dispatch == null) {
       return;
   }
   // ... later ...
   if (c == TerminalOutput.KEY_CTRL_C) {
       dispatch.accept(Message.BareMessage.CANCEL_BUILD_SINGLETON);
   }
   ```
   
   This pattern eliminates the race condition and makes the code more robust.
   
   _Claude Code on behalf of Guillaume Nodet_



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