[CP-SAT] Duplicate timer registration in StartDirectSolver when UserTimer aliases WallTimer

Open
#5,659 5 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@lperron is already working on this.

Since Nov 30, 2026.

  • #5660 by @PCTEJA — open

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp, python
Domain
backend, testing

Research direction

Inspect ortools/base/timer.h, ortools/sat/model.h, and the timer registrations in ortools/sat/cp_model_solver.cc, then reproduce the failure with the provided CP-SAT Python example using two workers and direct solve enabled. Confirm that duplicate WallTimer/UserTimer registration violates the singleton invariant, apply the minimal conditional registration fix, and add or run a regression test confirming the solver returns OPTIMAL with objective value 1.

Written by the indexing model from the issue text.

Description

Solver: CP-SAT Solver
Version / Commit

OR-Tools commit bf6d356392ad4230ae55230a2256407aa0de84a8.

The same issue is present on the public main branch as inspected on November 29, 2026.

Root Cause

DefaultCpModelSolver::StartDirectSolver() registers both WallTimer and UserTimer in the same model.

However, ortools/base/timer.h:65 defines:

typedef WallTimer UserTimer;

Consequently, both registrations use the same C++ type ID, violating the singleton invariant in Model::Register().

Affected locations:

  • ortools/base/timer.h:65
  • ortools/sat/cp_model_solver.cc:3965–3966
  • ortools/sat/model.h:195–199
Proposed Minimal Reproduction
from ortools.sat.python import cp_model

model = cp_model.CpModel()
x = model.new_bool_var("x")
model.maximize(x)

solver = cp_model.CpSolver()
solver.parameters.num_workers = 2
solver.parameters.cp_model_presolve = True
solver.parameters.cp_model_direct_solve = True
solver.parameters.log_search_progress = True

print(solver.solve(model))
Expected Behavior

The solver returns OPTIMAL with objective value 1.

Predicted Failure
CHECK(!singletons_.contains(type_id))

The conflicting registrations have been verified by source inspection. The minimal reproducer has not yet been executed on a pristine upstream build.

Suggested Fix
-    direct_solver_model_->Register<UserTimer>(model_->GetOrCreate<UserTimer>());
+    if constexpr (!std::is_same_v<WallTimer, UserTimer>) {
+      direct_solver_model_->Register<UserTimer>(
+          model_->GetOrCreate<UserTimer>());
+    }

The source file already includes <type_traits>.

This preserves compatibility if UserTimer becomes a distinct type in the future.

A regression test exercising direct solve with at least two workers is recommended. The proposed patch has not yet been build-tested.

Dominant language
C++
Stars
14.5k
Forks
2.6k
Avg merge
9h 43m
Merged PRs (30d)
53

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/or-tools

All issues in google/or-tools

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.