From 2c142d77afb37fb9ce5eace62d9f054013b3a2ee Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 11 Jan 2021 15:47:34 +0000 Subject: [PATCH] Clear PID when child process ends --- source/backend/plugin/CarlaPluginBridge.cpp | 2 +- source/backend/plugin/CarlaPluginJack.cpp | 2 +- source/backend/plugin/CarlaPluginLADSPADSSI.cpp | 2 +- source/modules/water/threads/ChildProcess.cpp | 16 ++++++++++++---- source/modules/water/threads/ChildProcess.h | 4 ++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/source/backend/plugin/CarlaPluginBridge.cpp b/source/backend/plugin/CarlaPluginBridge.cpp index f6d1be68f..82757d479 100644 --- a/source/backend/plugin/CarlaPluginBridge.cpp +++ b/source/backend/plugin/CarlaPluginBridge.cpp @@ -351,7 +351,7 @@ protected: else { // forced quit, may have crashed - if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/) + if (fProcess->getExitCodeAndClearPID() != 0) { carla_stderr("CarlaPluginBridgeThread::run() - bridge crashed"); diff --git a/source/backend/plugin/CarlaPluginJack.cpp b/source/backend/plugin/CarlaPluginJack.cpp index 2abcc7577..928509075 100644 --- a/source/backend/plugin/CarlaPluginJack.cpp +++ b/source/backend/plugin/CarlaPluginJack.cpp @@ -432,7 +432,7 @@ protected: else { // forced quit, may have crashed - if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/) + if (fProcess->getExitCodeAndClearPID() != 0) { carla_stderr("CarlaPluginJackThread::run() - application crashed"); diff --git a/source/backend/plugin/CarlaPluginLADSPADSSI.cpp b/source/backend/plugin/CarlaPluginLADSPADSSI.cpp index 86494b3be..8e46b0860 100644 --- a/source/backend/plugin/CarlaPluginLADSPADSSI.cpp +++ b/source/backend/plugin/CarlaPluginLADSPADSSI.cpp @@ -225,7 +225,7 @@ public: carla_stdout("CarlaThreadDSSIUI::run() - UI auto-closed successfully"); } } - else if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/) + else if (fProcess->getExitCodeAndClearPID() != 0) carla_stderr("CarlaThreadDSSIUI::run() - UI crashed while running"); else carla_stdout("CarlaThreadDSSIUI::run() - UI closed cleanly"); diff --git a/source/modules/water/threads/ChildProcess.cpp b/source/modules/water/threads/ChildProcess.cpp index 04f987062..2ca1b9514 100644 --- a/source/modules/water/threads/ChildProcess.cpp +++ b/source/modules/water/threads/ChildProcess.cpp @@ -54,9 +54,15 @@ public: } ~ActiveProcess() + { + closeProcessInfo(); + } + + void closeProcessInfo() noexcept { if (ok) { + ok = false; CloseHandle (processInfo.hThread); CloseHandle (processInfo.hProcess); } @@ -88,10 +94,11 @@ public: return TerminateProcess (processInfo.hProcess, 0) != FALSE; } - uint32 getExitCode() const noexcept + uint32 getExitCodeAndClearPID() noexcept { DWORD exitCode = 0; GetExitCodeProcess (processInfo.hProcess, &exitCode); + closeProcessInfo(); return (uint32) exitCode; } @@ -200,12 +207,13 @@ public: return ::kill (childPID, SIGTERM) == 0; } - uint32 getExitCode() const noexcept + uint32 getExitCodeAndClearPID() noexcept { if (childPID != 0) { int childState = 0; const int pid = waitpid (childPID, &childState, WNOHANG); + childPID = 0; if (pid >= 0 && WIFEXITED (childState)) return WEXITSTATUS (childState); @@ -246,9 +254,9 @@ bool ChildProcess::terminate() return activeProcess == nullptr || activeProcess->terminateProcess(); } -uint32 ChildProcess::getExitCode() const +uint32 ChildProcess::getExitCodeAndClearPID() { - return activeProcess != nullptr ? activeProcess->getExitCode() : 0; + return activeProcess != nullptr ? activeProcess->getExitCodeAndClearPID() : 0; } bool ChildProcess::waitForProcessToFinish (const int timeoutMs) diff --git a/source/modules/water/threads/ChildProcess.h b/source/modules/water/threads/ChildProcess.h index 314a5db89..2f8c77a6f 100644 --- a/source/modules/water/threads/ChildProcess.h +++ b/source/modules/water/threads/ChildProcess.h @@ -81,8 +81,8 @@ public: /** Blocks until the process is no longer running. */ bool waitForProcessToFinish (int timeoutMs); - /** If the process has finished, this returns its exit code. */ - uint32 getExitCode() const; + /** If the process has finished, this returns its exit code and also clears assigned PID. */ + uint32 getExitCodeAndClearPID(); /** Attempts to kill the child process. Returns true if it succeeded. Trying to read from the process after calling this may