Skip to content

Commit

Permalink
Merge pull request #1250 from adriendelsalle/fix-reproc-oom
Browse files Browse the repository at this point in the history
Improve catching of `reproc` errors
  • Loading branch information
wolfv authored Nov 3, 2021
2 parents 88c6b7c + 3c86959 commit ca36829
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
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

0 comments on commit ca36829

Please sign in to comment.