From 030d8f645fdc944b043cfa2c7caf42b6bad78344 Mon Sep 17 00:00:00 2001 From: Doug Simon Date: Mon, 18 Nov 2024 18:20:27 +0100 Subject: [PATCH 1/2] exit CompilerDaemon when a SocketException is received --- .../mxtool/compilerserver/CompilerDaemon.java | 28 +++++++++++++------ src/mx/_impl/mx.py | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java b/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java index 2eb364f5..66642ed7 100644 --- a/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java +++ b/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java @@ -25,6 +25,7 @@ package com.oracle.mxtool.compilerserver; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; @@ -152,14 +153,7 @@ public void run() { String requestOrigin = connectionSocket.getInetAddress().getHostAddress(); String prefix = String.format("[%s:%s] ", Instant.now(), requestOrigin); if (request == null || request.equals(REQUEST_HEADER_SHUTDOWN)) { - logf("%sShutting down%n", prefix); - running = false; - while (threadPool.getActiveCount() > 1) { - threadPool.awaitTermination(50, TimeUnit.MILLISECONDS); - } - serverSocket.close(); - // Just to be sure... - System.exit(0); + shutdownAndExit(prefix); } else if (request.startsWith(REQUEST_HEADER_COMPILE)) { String commandLine = request.substring(REQUEST_HEADER_COMPILE.length()); String[] args = commandLine.split("\u0000"); @@ -188,9 +182,27 @@ public void run() { input.close(); connectionSocket.close(); } + } catch (SocketException se) { + // Lost connection to mx + shutdownAndExit(""); } catch (Exception ioe) { ioe.printStackTrace(); } } + + private void shutdownAndExit(String prefix) { + logf("%sShutting down%n", prefix); + running = false; + try { + while (threadPool.getActiveCount() > 1) { + threadPool.awaitTermination(50, TimeUnit.MILLISECONDS); + } + serverSocket.close(); + } catch (InterruptedException | IOException e) { + e.printStackTrace(); + } + // Just to be sure... + System.exit(0); + } } } diff --git a/src/mx/_impl/mx.py b/src/mx/_impl/mx.py index f68c9c21..8fab3882 100755 --- a/src/mx/_impl/mx.py +++ b/src/mx/_impl/mx.py @@ -18224,7 +18224,7 @@ def alarm_handler(signum, frame): _CACHE_DIR = get_env('MX_CACHE_DIR', join(dot_mx_dir(), 'cache')) # The version must be updated for every PR (checked in CI) and the comment should reflect the PR's issue -version = VersionSpec("7.35.1") # [GR-60117] Enable build support for JEP 493 enabled base JDKs +version = VersionSpec("7.35.2") # [GR-59878] Exit CompilerDaemon when a SocketException is received _mx_start_datetime = datetime.utcnow() From bd750449d08f4a679c68386243fa9d2cf8c1f4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Pejovi=C4=87?= Date: Wed, 27 Nov 2024 13:53:30 +0100 Subject: [PATCH 2/2] Move thread pool shutdown to main thread --- .../mxtool/compilerserver/CompilerDaemon.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java b/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java index 66642ed7..d5417822 100644 --- a/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java +++ b/java/com.oracle.mxtool.compilerserver/src/com/oracle/mxtool/compilerserver/CompilerDaemon.java @@ -103,10 +103,14 @@ public Thread newThread(Runnable runnable) { if (running) { e.printStackTrace(); } else { - // Socket was closed + // we're shutting down } } } + threadPool.shutdown(); + while (!threadPool.isTerminated()) { + threadPool.awaitTermination(50, TimeUnit.MILLISECONDS); + } } private static void usage() { @@ -153,7 +157,7 @@ public void run() { String requestOrigin = connectionSocket.getInetAddress().getHostAddress(); String prefix = String.format("[%s:%s] ", Instant.now(), requestOrigin); if (request == null || request.equals(REQUEST_HEADER_SHUTDOWN)) { - shutdownAndExit(prefix); + shutdown(prefix); } else if (request.startsWith(REQUEST_HEADER_COMPILE)) { String commandLine = request.substring(REQUEST_HEADER_COMPILE.length()); String[] args = commandLine.split("\u0000"); @@ -171,8 +175,7 @@ public void run() { int unrecognizedRequestCount = unrecognizedRequests.incrementAndGet(); System.err.printf("%sUnrecognized request %d (len=%d): \"%s\"%n", prefix, unrecognizedRequestCount, request.length(), printable(request)); if (unrecognizedRequestCount > MAX_UNRECOGNIZED_REQUESTS) { - System.err.printf("%sShutting down after receiving %d unrecognized requests%n", prefix, unrecognizedRequestCount); - System.exit(0); + shutdown(String.format("%sReceived %d unrecognized requests: ", prefix, unrecognizedRequestCount)); } output.write("-1\n"); } @@ -184,25 +187,20 @@ public void run() { } } catch (SocketException se) { // Lost connection to mx - shutdownAndExit(""); + shutdown(""); } catch (Exception ioe) { ioe.printStackTrace(); } } + } - private void shutdownAndExit(String prefix) { - logf("%sShutting down%n", prefix); - running = false; - try { - while (threadPool.getActiveCount() > 1) { - threadPool.awaitTermination(50, TimeUnit.MILLISECONDS); - } - serverSocket.close(); - } catch (InterruptedException | IOException e) { - e.printStackTrace(); - } - // Just to be sure... - System.exit(0); + private void shutdown(String prefix) { + logf("%sShutting down%n", prefix); + running = false; + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); } } }