From 0b2e958253d379c35776df1303a9420501d229cc Mon Sep 17 00:00:00 2001 From: Justin Parker Date: Tue, 7 Mar 2023 16:36:51 +1100 Subject: [PATCH] bugfix #289 - not ready to call process.exitValue --- .../main/java/org/nodel/toolkit/ManagedProcess.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/nodel-framework/src/main/java/org/nodel/toolkit/ManagedProcess.java b/nodel-framework/src/main/java/org/nodel/toolkit/ManagedProcess.java index 71c8fc73..13f99247 100644 --- a/nodel-framework/src/main/java/org/nodel/toolkit/ManagedProcess.java +++ b/nodel-framework/src/main/java/org/nodel/toolkit/ManagedProcess.java @@ -715,7 +715,9 @@ private void launchAndRead() throws Exception { // fire the STOPPED handler if it had fully started previously if (os != null) - Handler.tryHandle(_stoppedCallback, process.exitValue(), _callbackErrorHandler); + // .waitFor() is required instead of .exitValue() because it is possible (but rare) to get here + // with the process not fully dead and cleaned up yet + Handler.tryHandle(_stoppedCallback, process.waitFor(), _callbackErrorHandler); // propagate exception only on unusual termination if (_state == State.Started) @@ -835,7 +837,7 @@ private void readTextLoop(Process process) throws Exception { handleReceivedData(str); // then fire the stopped event and pass through the exit value - Handler.tryHandle(_stoppedCallback, _process.waitFor(), _callbackErrorHandler); + Handler.tryHandle(_stoppedCallback, process.waitFor(), _callbackErrorHandler); } } @@ -901,7 +903,7 @@ public void run() { * No read-delimiters specified, so fire events as data segments arrive. * (suppress was used with resource-free CountableInputStream) */ - private void readUnboundedRawLoop(Process process) throws IOException { + private void readUnboundedRawLoop(Process process) throws Exception { InputStream stdout = process.getInputStream(); @SuppressWarnings("resource") @@ -926,8 +928,8 @@ private void readUnboundedRawLoop(Process process) throws IOException { // the peer has gracefully closed down the connection or we're shutting down if (!_shutdown) { - // then fire the disconnected callback - Handler.tryHandle(_stoppedCallback, _process.exitValue(), _callbackErrorHandler); + // then fire the stopped callback + Handler.tryHandle(_stoppedCallback, process.waitFor(), _callbackErrorHandler); } }