Python (v10.0-rc.1): Model.restore_assignment() and Assignment.objective() take ownership of C++-owned objects (segfault)

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

Maintainers usually reply within 1 day

@Mizux is already working on this.

Since Dec 1, 2026.

  • #5671 by @yoonkh2000 — open

Assessment

This issue has not been assessed yet.

Description

Lang: Python Solver: Routing & CP Solver

What version of OR-Tools and what language are you using?
Version: v10.0-rc.1 (wheel ortools-10.0.0rc1-cp313-cp313-macosx_11_0_arm64.whl from PyPI). The same binding code is on main (b11de38f0).
Language: Python

Which solver are you using (e.g. CP-SAT, Routing Solver, GLOP, BOP, Gurobi)
Routing Solver

What operating system (Linux, Windows, ...) and version?
macOS 27.2.1 (arm64), Python 3.13.2

What did you do?
While migrating from pywrapcp (v9.15) to ortools.routing.python.routing, two calls crash the interpreter with a segmentation fault. Both return objects that C++ owns, but the pybind11 bindings hand them to Python with take_ownership, so Python deletes them when the wrapper is collected.

Python method C++ returns Owner Binding policy
Model.restore_assignment() collect_assignments_->solution(0) (Model::DoRestoreAssignment) the model's solution collector take_ownership
Model.read_assignment() same (DoRestoreAssignment) the model's solution collector take_ownership
Assignment.objective(), Assignment.objective_from_index() the cost IntVar* the solver none, so pybind11 uses take_ownership for a raw pointer
  1. restore_assignment() before the first solve (a warm start):
import gc
from ortools.constraint_solver.python import constraint_solver
from ortools.routing.python import routing

manager = routing.IndexManager(6, 2, 0)
model = routing.Model(manager)
model.set_arc_cost_evaluator_of_all_vehicles(
    model.register_transit_callback(
        lambda i, j: abs(manager.index_to_node(i) - manager.index_to_node(j))))
model.close_model()
seed = constraint_solver.Assignment(model.solver)
model.routes_to_assignment([[1, 2], [3, 4, 5]], False, True, seed)
restored = model.restore_assignment(seed)
del restored
gc.collect()
del model, seed
gc.collect()  # Fatal Python error: Segmentation fault

The native stack is ~Model → ~Solver → Solver::PopState → Trail::BacktrackTo → ~BestValueSolutionCollector: the collector frees the assignment that Python already deleted. When the same pointer was returned earlier by solve() (reference policy), pybind11 reuses that wrapper and the bug does not show, which is why it appears only on the warm start path.

  1. Assignment.objective():
import gc
from ortools.routing.python import routing

manager = routing.IndexManager(4, 1, 0)
model = routing.Model(manager)
model.set_arc_cost_evaluator_of_all_vehicles(model.register_transit_callback(lambda i, j: 1))
solution = model.solve()
objective = solution.objective()
del objective
gc.collect()
model.solve()  # Fatal Python error: Segmentation fault

The native stack is Solver::Solve → Solver::NextSolution → Solver::ProcessConstraints → SafeSumConstraint::Post: the next search re-posts the constraints on the cost variable that Python deleted.

What did you expect to see
Both scripts finish. In v9.15 (pywrapcp) the equivalent calls do not take ownership.

What did you see instead?
Fatal Python error: Segmentation fault at the lines marked above.

Anything else we should know about your project / environment
read_assignment_from_routes() and the solve*() methods already use reference or reference_internal for the same kind of pointer. compact_assignment() correctly keeps take_ownership, because CompactAssignmentInternal returns a new assignment. I will open a PR that switches the three methods above to reference_internal and adds regression tests.

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.