From a7c897a29fd8b65e110018e27186f6b7b6983f9c Mon Sep 17 00:00:00 2001 From: Oliver Reiche Date: Tue, 19 Dec 2023 15:59:56 +0100 Subject: SystemCommand: Fix handling of child status (cherry-picked from commit c3d28a4cc3115644414ddba41d4b7ada5fd74fc2) --- CHANGELOG.md | 1 + 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 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* v) noexcept -- cgit v1.2.3