Keep Python opaque data alive for the lifetime of the request - #2445
Open
Gopalakrishnan Nallasamy (GopalakrishnanN) wants to merge 1 commit into
Open
Conversation
Request.SetOpaqueData() stores the PyObject* unowned by design, and the Python
binding passed opaque_data.ptr() straight through without holding a reference.
Anything that is not independently kept alive by the caller is therefore freed
as soon as set_opaque_data() returns, and get_opaque_data() hands back a dangling
pointer.
The natural way to write it is a use-after-free:
req.set_opaque_data(Sink(prompt)) # temporary, freed immediately
...
sink = ready.get_opaque_data() # dangling
sink.text += ... # general protection fault in libpython
That faults inside libpython rather than raising, so it looks like a runtime bug
rather than an ownership mistake. The shipped continuous-batching example only
avoids it because it passes a long-lived RequestPool.
Add pybind11::keep_alive<1, 2> so the object lives as long as the request that
references it.
Gopalakrishnan Nallasamy (GopalakrishnanN)
requested a review
from a team
as a code owner
August 21, 2026 03:02
Copilot started reviewing on behalf of
Gopalakrishnan Nallasamy (GopalakrishnanN)
August 21, 2026 03:02
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a Python binding lifetime hazard in ONNX Runtime GenAI’s request “opaque data” feature by ensuring Python objects passed to Request.set_opaque_data() are kept alive for as long as the Request instance that references them, preventing use-after-free crashes when retrieving the pointer later.
Changes:
- Adds a
pybind11::keep_alive<1, 2>call policy toRequest.set_opaque_data()so the Python object’s lifetime is tied to theRequest. - Adds an in-code comment explaining the unowned
PyObject*storage in the core and why the binding must enforce lifetime.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Request.SetOpaqueData() stores the PyObject* unowned by design, and the Python binding passed opaque_data.ptr() straight through without holding a reference. Anything that is not independently kept alive by the caller is therefore freed as soon as set_opaque_data() returns, and get_opaque_data() hands back a dangling pointer.
The natural way to write it is a use-after-free:
That faults inside libpython rather than raising, so it looks like a runtime bug rather than an ownership mistake. The shipped continuous-batching example only avoids it because it passes a long-lived RequestPool.
Add pybind11::keep_alive<1, 2> so the object lives as long as the request that references it.