onnxruntime
82395902 - Pin C# RunAsync arguments until completion (#32015)

Commit
2 days ago
Pin C# RunAsync arguments until completion (#32015) This pull request improves the memory management and safety of async inference execution in the ONNX Runtime C# API, particularly around the `RunAsync` method. The main changes ensure that arrays passed to native code are properly pinned and unpinned, reducing the risk of memory errors, and that resources are disposed of correctly in both success and failure scenarios. The test code is also updated to more rigorously exercise object lifetimes. ### Memory management and resource safety improvements * The `CallbackHost` class now implements `IDisposable` and manages the pinning and unpinning of input and output arrays using `GCHandle`, ensuring that native code receives stable pointers and that handles are freed even on exceptions. [[1]](diffhunk://#diff-622c8600020a433468d4a0e82a320fdc478e0c685c2d92c86c04dac351af4d04L1146-R1150) [[2]](diffhunk://#diff-622c8600020a433468d4a0e82a320fdc478e0c685c2d92c86c04dac351af4d04R1162-R1176) [[3]](diffhunk://#diff-622c8600020a433468d4a0e82a320fdc478e0c685c2d92c86c04dac351af4d04R1188-R1268) * The `OrtCallback` and `RunAsyncInternal` methods are updated to explicitly dispose of the `CallbackHost` and free handles in all code paths, further strengthening resource safety. [[1]](diffhunk://#diff-622c8600020a433468d4a0e82a320fdc478e0c685c2d92c86c04dac351af4d04R1135) [[2]](diffhunk://#diff-622c8600020a433468d4a0e82a320fdc478e0c685c2d92c86c04dac351af4d04R1188-R1268) ### Native interop changes * The native P/Invoke signature for `OrtRunAsync` is updated to pass pointers (`IntPtr`) instead of managed arrays, matching the new pinned memory approach. * Calls to the native API (`OrtRunAsync`) are updated to use the new pointer properties from `CallbackHost` instead of directly passing arrays. ### Test improvements * The async inference test now explicitly creates and nulls out a `RunOptions` instance and forces garbage collection, testing that managed resources are not prematurely collected while native work is outstanding. These changes collectively make async inference safer and more robust, especially in scenarios with concurrent or long-running operations.
Author
Parents
Loading