From a9587b39ea6776b1d1324288786176102f65cee5 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 10 Feb 2025 19:56:16 +0100 Subject: solver3: Implement a timeout, default 10s A SAT solver can run more or less forever, but that's not a good user experience. --- apt-pkg/solver3.cc | 8 +++++++- apt-pkg/solver3.h | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'apt-pkg') diff --git a/apt-pkg/solver3.cc b/apt-pkg/solver3.cc index b4a69d844..d363cdee7 100644 --- a/apt-pkg/solver3.cc +++ b/apt-pkg/solver3.cc @@ -32,8 +32,9 @@ #include #include -#include #include +#include +#include #include // FIXME: Helpers stolen from DepCache, please give them back. @@ -740,6 +741,10 @@ bool APT::Solver::Pop() for (std::string msg; _error->PopMessage(msg);) std::cerr << "Branch failed: " << msg << std::endl; + time_t now = time(nullptr); + if (now - startTime >= Timeout) + return _error->Error("Solver timed out."); + _error->RevertToStack(); assert(choices.back() < solved.size()); @@ -834,6 +839,7 @@ void APT::Solver::RescoreWorkIfNeeded() bool APT::Solver::Solve() { + startTime = time(nullptr); while (true) { while (not Propagate()) diff --git a/apt-pkg/solver3.h b/apt-pkg/solver3.h index 34da03ea0..da75179d5 100644 --- a/apt-pkg/solver3.h +++ b/apt-pkg/solver3.h @@ -213,6 +213,9 @@ class Solver // This is an index into the solved vector. std::vector choices{}; + // \brief The time we called Solve() + time_t startTime; + /// Various configuration options // \brief Debug level int debug{_config->FindI("Debug::APT::Solver")}; @@ -232,6 +235,8 @@ class Solver bool FixPolicyBroken{_config->FindB("APT::Get::Fix-Policy-Broken")}; // \brief If set, we use strict pinning. bool DeferVersionSelection{_config->FindB("APT::Solver::Defer-Version-Selection", true)}; + // \brief If set, we use strict pinning. + int Timeout{_config->FindI("APT::Solver::Timeout", 10)}; // \brief Discover a variable, translating the underlying dependencies to the SAT presentation // -- cgit v1.2.3-70-g09d2