diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/buildtool/system/system_command.hpp | 26 |
2 files changed, 24 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e499691c..87d4f607 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Bug fixes on top of release `1.2.4`. - On errors reading expressions, the full import path is reported in the error message. +- Child processes are now properly waited for in all circumstances. ## Release `1.2.4` (2023-12-19) diff --git a/src/buildtool/system/system_command.hpp b/src/buildtool/system/system_command.hpp index 660d9202..b9145301 100644 --- a/src/buildtool/system/system_command.hpp +++ b/src/buildtool/system/system_command.hpp @@ -201,10 +201,30 @@ class SystemCommand { // wait for child to finish and obtain return value int status{}; - ::waitpid(pid, &status, 0); + std::optional<int> retval{std::nullopt}; + do { + if (::waitpid(pid, &status, 0) == -1) { + // this should never happen + logger_.Emit(LogLevel::Error, + "Waiting for child failed with: {}", + strerror(errno)); + break; + } + + if (WIFEXITED(status)) { // NOLINT(hicpp-signed-bitwise) + retval = WEXITSTATUS(status); // NOLINT(hicpp-signed-bitwise) + } + else if (WIFSIGNALED(status)) { // NOLINT(hicpp-signed-bitwise) + constexpr auto kSignalBit = 128; + auto sig = WTERMSIG(status); // NOLINT(hicpp-signed-bitwise) + retval = kSignalBit + sig; + logger_.Emit( + LogLevel::Debug, "Child got killed by signal {}", sig); + } + // continue waitpid() in case we got STOPSIG from child + } while (not retval); - // NOLINTNEXTLINE(hicpp-signed-bitwise) - return WEXITSTATUS(status); + return retval; } static auto UnwrapStrings(std::vector<std::string>* v) noexcept |