Retain Python async run resources - #32041
Merged
Akshay Sonawane (apsonawane) merged 2 commits intoAug 20, 2026
Merged
Conversation
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 12, 2026 21:40
View session
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 12, 2026 21:41
Contributor
There was a problem hiding this comment.
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
AsyncResourceto hold strong references to feed Python objects, the session, and run options for the duration ofRunAsync. - Update the
run_asyncbinding to acceptpy::objectfor 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.
Ti-Tai Wang (titaiwangms)
previously approved these changes
Aug 13, 2026
Ti-Tai Wang (titaiwangms)
approved these changes
Aug 20, 2026
auto-merge was automatically disabled
August 20, 2026 17:53
Base branch was modified
Akshay Sonawane (apsonawane)
deleted the
fix/python-run-async-input-lifetime
branch
August 20, 2026 17:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_asyncare properly referenced and not prematurely garbage collected, and that the API is more Pythonic by acceptingpy::objectfor 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:
AsyncResourcestruct 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).run_asyncbinding now acceptspy::objectfor 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).run_async, the code now stores each input object infeed_objectsbefore creating the correspondingOrtValue, ensuring the Python objects are not garbage collected too early (onnxruntime/python/onnxruntime_pybind_state.cc).Testing and validation:
weakrefwas added to verify that input arrays passed torun_asyncremain alive until the callback is invoked, preventing premature garbage collection (onnxruntime/test/python/onnxruntime_test_python.py) [1] [2] [3].onnxruntime/test/python/onnxruntime_test_python.py).