From e81959e4f1fc9d7db535939488a7da6524a97f79 Mon Sep 17 00:00:00 2001 From: 0x7d8 Date: Fri, 16 Aug 2024 11:27:49 +0200 Subject: [PATCH] handle exiting of child process properly --- .../server/ServerBootstrap.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/dev/mcvapi/neoforgeserverjar/server/ServerBootstrap.java b/src/main/java/dev/mcvapi/neoforgeserverjar/server/ServerBootstrap.java index 3ca042a..dc1aaef 100644 --- a/src/main/java/dev/mcvapi/neoforgeserverjar/server/ServerBootstrap.java +++ b/src/main/java/dev/mcvapi/neoforgeserverjar/server/ServerBootstrap.java @@ -24,7 +24,7 @@ public void startServer(String[] cmd) throws ServerStartupException { processHolder.stdoutReader = new BufferedReader(new InputStreamReader(processHolder.process.getInputStream())); processHolder.stderrReader = new BufferedReader(new InputStreamReader(processHolder.process.getErrorStream())); - new Thread(() -> { + Thread stdoutThread = new Thread(() -> { try { String line; while ((line = processHolder.stdoutReader.readLine()) != null) { @@ -33,9 +33,10 @@ public void startServer(String[] cmd) throws ServerStartupException { } catch (IOException e) { e.printStackTrace(); } - }).start(); + }); + stdoutThread.start(); - new Thread(() -> { + Thread stderrThread = new Thread(() -> { try { String line; while ((line = processHolder.stderrReader.readLine()) != null) { @@ -44,9 +45,10 @@ public void startServer(String[] cmd) throws ServerStartupException { } catch (IOException e) { e.printStackTrace(); } - }).start(); + }); + stderrThread.start(); - new Thread(() -> { + Thread stdinThread = new Thread(() -> { try (BufferedReader userInputReader = new BufferedReader(new InputStreamReader(System.in))) { String userInput; while ((userInput = userInputReader.readLine()) != null) { @@ -56,7 +58,8 @@ public void startServer(String[] cmd) throws ServerStartupException { } catch (IOException e) { e.printStackTrace(); } - }).start(); + }); + stdinThread.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { if (processHolder.process != null && processHolder.process.isAlive()) { @@ -66,18 +69,23 @@ public void startServer(String[] cmd) throws ServerStartupException { processHolder.process.waitFor(); } catch (IOException | InterruptedException e) { + e.printStackTrace(); Thread.currentThread().interrupt(); + } finally { + processHolder.process.destroy(); } } })); - try { - processHolder.process.waitFor(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } catch (IOException exception) { - throw new ServerStartupException("Failed to start the NeoForge server.", exception); + int exitCode = processHolder.process.waitFor(); + + stdoutThread.join(); + stderrThread.join(); + stdinThread.interrupt(); + + System.exit(exitCode); + } catch (IOException | InterruptedException exception) { + throw new ServerStartupException("Failed to start or monitor the NeoForge server.", exception); } finally { if (processHolder.process != null && processHolder.process.isAlive()) { try {