Skip to content

Conversation

@sungeunk
Copy link
Contributor

@sungeunk sungeunk commented Oct 22, 2025

This PR fixes a critical memory safety issue where a C++ function was returning a const & reference to Python without proper lifetime management.

The Problem: The returned reference's lifetime was tied to a parent C++ object. When the parent object was garbage-collected in Python, the reference became a dangling pointer, leading to unpredictable segmentation faults (use-after-free).

The Solution: This has been resolved by applying the py::return_value_policy::reference_internal policy to the binding. This ensures the parent object's lifetime is extended as long as the returned reference is in use by Python, preventing any memory corruption.

Tickets:

  • 174729

@sungeunk sungeunk requested a review from a team as a code owner October 22, 2025 07:25
@github-actions github-actions bot added the category: Python API OpenVINO Python bindings label Oct 22, 2025
@mlukasze mlukasze requested a review from Copilot October 22, 2025 07:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a lifecycle management issue in the Python bindings for OpenVINO's CompiledModel class by adding py::return_value_policy::reference_internal to three overloaded output() method bindings. This ensures that returned output objects maintain a proper reference to their parent CompiledModel instance, preventing premature deallocation and potential use-after-free scenarios.

Key Changes:

  • Added py::return_value_policy::reference_internal to all three output() method overloads (no-arg, by-index, and by-name variants)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@sungeunk sungeunk changed the title add py::return_value_policy::reference_internal to fix lifecycle issue Fix: Prevent dangling reference crash by correcting C++ return value policy. Oct 22, 2025
@sungeunk
Copy link
Contributor Author

sungeunk commented Oct 23, 2025

@mlukasze Could you please merge this PR when you have a moment?

@almilosz almilosz added this pull request to the merge queue Oct 23, 2025
Merged via the queue into openvinotoolkit:master with commit d349f15 Oct 23, 2025
207 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: Python API OpenVINO Python bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants