Skip to content

Retain Python async run resources - #32041

Merged
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/python-run-async-input-lifetime
Aug 20, 2026
Merged

Retain Python async run resources#32041
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/python-run-async-input-lifetime

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request updates the ONNX Runtime Python bindings to improve memory management and API flexibility for asynchronous inference sessions. The main changes ensure that Python objects used as inputs to run_async are properly referenced and not prematurely garbage collected, and that the API is more Pythonic by accepting py::object for session and run options. It also adds a test to verify that input arrays are kept alive during asynchronous execution.

Improvements to memory management and API flexibility:

  • The AsyncResource struct now stores references to Python feed objects, the session, and run options to ensure they are kept alive for the duration of async execution (onnxruntime/python/onnxruntime_pybind_state.cc).
  • The run_async binding now accepts py::object for the session and run options, and internally casts them as needed. This makes the API more Pythonic and flexible (onnxruntime/python/onnxruntime_pybind_state.cc).
  • When preparing feeds for run_async, the code now stores each input object in feed_objects before creating the corresponding OrtValue, ensuring the Python objects are not garbage collected too early (onnxruntime/python/onnxruntime_pybind_state.cc).

Testing and validation:

  • A test using weakref was added to verify that input arrays passed to run_async remain alive until the callback is invoked, preventing premature garbage collection (onnxruntime/test/python/onnxruntime_test_python.py) [1] [2] [3].
  • The test also exercises explicit deletion and garbage collection of inputs, session, and run options to ensure robustness (onnxruntime/test/python/onnxruntime_test_python.py).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the ONNX Runtime Python bindings’ InferenceSession.run_async path to retain the Python-owned resources (session, run options, and feed objects) for the full duration of asynchronous execution, preventing premature garbage collection of inputs used by run_async. It also adds a Python test that explicitly deletes references and forces GC to validate the lifetime behavior.

Changes:

  • Extend AsyncResource to hold strong references to feed Python objects, the session, and run options for the duration of RunAsync.
  • Update the run_async binding to accept py::object for session and run options, casting internally and preserving the objects.
  • Add a weakref-based regression test that deletes input/session/run options, triggers GC, and asserts the input is still alive in the callback.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
onnxruntime/python/onnxruntime_pybind_state.cc Retains Python objects (feeds/session/run_options) inside AsyncResource to prevent GC during async execution and updates run_async binding signature accordingly.
onnxruntime/test/python/onnxruntime_test_python.py Adds a weakref + forced GC test to verify run_async keeps input arrays alive until callback completion.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

auto-merge was automatically disabled August 20, 2026 17:53

Base branch was modified

@apsonawane
Akshay Sonawane (apsonawane) merged commit 1cb4307 into main Aug 20, 2026
104 of 107 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/python-run-async-input-lifetime branch August 20, 2026 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants