Python (v10.0-rc.1): Model.restore_assignment() and Assignment.objective() take ownership of C++-owned objects (segfault)
Maintainers usually reply within 1 day
Assessment
This issue has not been assessed yet.
Description
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 |
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.
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/or-tools
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 42/100
Maintainers usually reply within 1 day
-
CP-SAT: TimeTableEdgeFinding can report false INFEASIBLE for large cumulative scheduling modelsPossibly taken @lperron claimed this today. OpenBug Solver: CP-SAT Solver
google/or-tools#5672 · 2 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
[CP-SAT] Duplicate timer registration in StartDirectSolver when UserTimer aliases WallTimerPossibly taken @lperron claimed this 1 day ago. OpenSolver: CP-SAT Solver
Difficulty 3/5 Half a day Newbie friendliness 85/100
google/or-tools#5659 · 5 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Maintainers usually reply within 1 day
Similar issues
-
c++ easy first-contributor first-contributors good first issue help wanted up-for-grabs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
fork-commit-merge/fork-commit-merge#8709 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
Type: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
ClickHouse/clickhouse-odbc#680 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ionut-mihalache/aqua#31 · 1 comment ·