From f331e31505ab78620de2670b19bdefaee395b4e8 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 18 Feb 2025 14:31:48 +0100 Subject: solver3: Fix error stack handling Pushing the stack in push and popping it in pop did not really work correctly and is more complex than needed. Instead, push the error stack at the start of the Solve() method and revert at the end, such that we leave exactly at the same error stack level we entered. To handle error clearing on backtracking, just discard any pending errors. --- apt-pkg/solver3.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/apt-pkg/solver3.cc b/apt-pkg/solver3.cc index 233d7c712..7117296ab 100644 --- a/apt-pkg/solver3.cc +++ b/apt-pkg/solver3.cc @@ -673,8 +673,6 @@ void APT::Solver::Push(Work work) choices.push_back(solved.size()); solved.push_back(Solved{Var(), std::move(work)}); - // Pop() will call MergeWithStack() when reverting to level 0, or RevertToStack after dumping to the debug log. - _error->PushToStack(); } void APT::Solver::UndoOne() @@ -713,15 +711,15 @@ bool APT::Solver::Pop() if (depth() == 0) return false; - if (unlikely(debug >= 2)) - 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(); + if (unlikely(debug >= 2)) + for (std::string msg; _error->PopMessage(msg);) + std::cerr << "Branch failed: " << msg << std::endl; + + _error->Discard(); assert(choices.back() < solved.size()); int itemsToUndo = solved.size() - choices.back(); @@ -781,6 +779,8 @@ bool APT::Solver::AddWork(Work &&w) bool APT::Solver::Solve() { + _error->PushToStack(); + DEFER([&]() { _error->MergeWithStack(); }); startTime = time(nullptr); while (true) { -- cgit v1.2.3-70-g09d2