Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve catching of reproc errors #1250

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ macro(libmamba_create_target target_name linkage deps_linkage output_name)
${LIBSOLVEXT_BUILD_STATICRARIES}
${sodium_LIBRARY_RELEASE}
reproc++
reproc
)

add_definitions("-DLIBARCHIVE_STATIC -DCURL_STATICLIB -DSOLV_BUILD_STATIC")
Expand All @@ -324,6 +325,7 @@ macro(libmamba_create_target target_name linkage deps_linkage output_name)
${OPENSSL_LIBRARIES}
${YAML_CPP_LIBRARIES}
reproc++
reproc
)

target_link_libraries(${target_name} PUBLIC
Expand Down
3 changes: 3 additions & 0 deletions libmamba/include/mamba/core/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "nlohmann/json.hpp"

#include <reproc++/reproc.hpp>

#include <array>
#include <iomanip>
#include <limits>
Expand Down Expand Up @@ -402,6 +404,7 @@ namespace mamba

std::time_t parse_utc_timestamp(const std::string& timestamp);

void assert_reproc_success(const reproc::options& options, int status, std::error_code ec);
} // namespace mamba

#endif // MAMBA_UTIL_HPP
8 changes: 2 additions & 6 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ namespace mamba
<< " packages: " << join(", ", deps) << termcolor::reset;
LOG_INFO << "Calling: " << join(" ", install_args);

auto [_, ec] = reproc::run(wrapped_command, options);

if (ec)
{
throw std::runtime_error(ec.message());
}
auto [status, ec] = reproc::run(wrapped_command, options);
assert_reproc_success(options, status, ec);
}

auto& truthy_values()
Expand Down
36 changes: 36 additions & 0 deletions libmamba/src/core/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern "C"
#include <thread>
#include <condition_variable>

#include <reproc/reproc.h>

#include "mamba/core/context.hpp"
#include "mamba/core/util.hpp"
#include "mamba/core/output.hpp"
Expand Down Expand Up @@ -1093,4 +1095,38 @@ namespace mamba
}
return res;
}

bool reproc_killed(int status)
{
return status == REPROC_SIGKILL;
}

bool reproc_terminated(int status)
{
return status == REPROC_SIGTERM;
}

void assert_reproc_success(const reproc::options& options, int status, std::error_code ec)
{
bool killed_not_an_err = (options.stop.first.action == reproc::stop::kill)
|| (options.stop.second.action == reproc::stop::kill)
|| (options.stop.third.action == reproc::stop::kill);

bool terminated_not_an_err = (options.stop.first.action == reproc::stop::terminate)
|| (options.stop.second.action == reproc::stop::terminate)
|| (options.stop.third.action == reproc::stop::terminate);

if (ec || (!killed_not_an_err && reproc_killed(status))
|| (!terminated_not_an_err && reproc_terminated(status)))
{
if (ec)
LOG_ERROR << "Subprocess call failed: " << ec.message();
else if (reproc_killed(status))
LOG_ERROR << "Subprocess call failed (killed)";
else
LOG_ERROR << "Subprocess call failed (terminated)";
throw std::runtime_error("Subprocess call failed. Aborting.");
}
}

} // namespace mamba