Fix unbounded lifetime on WithOutputTensor in Rust bindings - #29251
Merged
Conversation
sayanshaw24
enabled auto-merge (squash)
June 25, 2026 16:33
apsonawane
approved these changes
Jun 26, 2026
This was referenced Jul 25, 2026
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.
Fix unbounded lifetime on WithOutputTensor in Rust bindings
Description
The
WithOutputTensor<'a, T>struct had a free lifetime parameter'aon itsTryFrom<OrtOutputTensor>impl that was unconstrained by any input. Combined with theDerefimpl (whoseTarget = ArrayView<'a, T, IxDyn>exposed aClone-able view), it was possible for theArrayViewto outlive the underlyingOrtOutputTensorbuffer owner.This change restructures
WithOutputTensorto eliminate the unbounded lifetime:'alifetime parameter fromWithOutputTensor,OrtOutput, andSession::runDerefimpl (the escape hatch)ArrayView<'a, T>with a raw pointer + shapeview(&self)method returningArrayView<'_, T, IxDyn>— the view lifetime is now tied to&self.view()Motivation
The C API contract (
onnxruntime_c_api.h) explicitly bounds the data pointer lifetime to theOrtValue: the pointer is only valid until the value is destroyed. The Rust type system must enforce this invariant. Previously it did not — theArrayViewcould be cloned out and observed after theOrtValuewas freed.API Change
Testing
Existing integration tests updated to use the new
view()API. The fix is enforced at compile time by the borrow checker — the previously problematic pattern now produces a lifetime error.