You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
No actual libtorch code is called in the repro, so I don't think that part is relevant. If the issue disappears after removing it from Cargo.toml, then there might be some weird link-level incompatibility between the two (protobuf?), which isn't fixable by ort.
By just including an enum Device(0), the compiler links libtorch. One of those dynamically linked libs from libtorch probably have static constructors that also register atexit.
tch -> libtorch will sometimes fire first and tear down its CUDA state. Next ort tries to release its onnx runtime environment, which internally uses CUDA, and if CUDA is already gone, then that's heap corruption.
I pushed to the repro a C program that does the exact same onnx runtime and CUDA work as main.rs. But releases the environment explicitly instead of deferring to atexit. No crash, ever.
Conveniently, no crash on Windows. It'll be a bit before I can try to repro on Linux; I'm currently sick and the last thing I want to do right now is wrestle with CUDA in WSL2 again 🙃
tch -> libtorch will sometimes fire first and tear down its CUDA state. Next ort tries to release its onnx runtime environment, which internally uses CUDA, and if CUDA is already gone, then that's heap corruption.
Torch only sets up its CUDA state once a CUDA operation is used, which isn't the case here. But a deconstructor somewhere is probably to blame.
Did the C test link to libtorch as well? Otherwise it's hard to tell if the bug disappeared because Torch wasn't linked or because of the different environment lifetime.
last thing I want to do right now is wrestle with CUDA in WSL2 again 🙃
I don't blame you 😆
Did the C test link to libtorch as well?
No because I wanted to test onnx runtime in isolation to show it's not to blame. I pushed a new commit that links libtorch. LD_DEBUG=libs ./c_main shows libc10_cuda.solibtorch_cuda.so and libcudart.so.12. Still no crash.
I thought maybe this would lead to an obvious solution but looks a bit more involved if you're not having issues on Windows. I'll have to come back to this issue at a later time if it remains open.
Also disappears with a debug build of ONNX Runtime 🙃🙃🙃
This bit from Valgrind (which I'm not even sure is causing the issue, given there are a million other CUDA-related invalid reads prior too) is all there is to go off of:
Details
Invalid read of size 8
at 0x23D9D97C: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime_providers_cuda.so)
by 0x21E66300: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E673D3: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E446DC: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E447AC: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E1FCEC: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x2170A9: <ort::environment::Environment as core::ops::drop::Drop>::drop (lib.rs:326)
by 0x2136B6: core::ptr::drop_in_place<ort::environment::Environment> (library/core/src/ptr/mod.rs:805)
by 0x223021: alloc::sync::Arc<T,A>::drop_slow (library/alloc/src/sync.rs:2120)
by 0x216F7F: <alloc::sync::Arc<T,A> as core::ops::drop::Drop>::drop (library/alloc/src/sync.rs:2854)
by 0x21467A: core::ptr::drop_in_place<alloc::sync::Arc<ort::environment::Environment>> (library/core/src/ptr/mod.rs:805)
by 0x212885: core::ptr::drop_in_place<core::option::Option<alloc::sync::Arc<ort::environment::Environment>>> (library/core/src/ptr/mod.rs:805)
Address 0x8d476768 is 8 bytes inside a block of size 64 free'd
at 0x484A61D: operator delete(void*, unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x1AFFBA75: __run_exit_handlers (exit.c:108)
by 0x1AFFBBBD: exit (exit.c:138)
by 0x1AFDE1D0: (below main) (libc_start_call_main.h:74)
Block was alloc'd at
at 0x4846FA3: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x21E59678: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x23D82286: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime_providers_cuda.so)
by 0x21E68919: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E6A72B: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E6A97C: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x22403D: <ort::ep::cuda::CUDA as ort::ep::ExecutionProvider>::register (lib.rs:329)
by 0x21C0BD: ort::ep::apply_execution_providers::register_inner (mod.rs:340)
by 0x21D77E: ort::ep::apply_execution_providers (mod.rs:363)
by 0x2012E5: ort::session::builder::impl_options::<impl ort::session::builder::SessionBuilder>::with_execution_providers (impl_options.rs:39)
by 0x1BE692: gpt2::main (gpt2.rs:14)
by 0x1CAF0A: core::ops::function::FnOnce::call_once (library/core/src/ops/function.rs:250)
Invalid read of size 4
at 0x23D9D9A2: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime_providers_cuda.so)
by 0x21E66300: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E673D3: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E446DC: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E447AC: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E1FCEC: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x2170A9: <ort::environment::Environment as core::ops::drop::Drop>::drop (lib.rs:326)
by 0x2136B6: core::ptr::drop_in_place<ort::environment::Environment> (library/core/src/ptr/mod.rs:805)
by 0x223021: alloc::sync::Arc<T,A>::drop_slow (library/alloc/src/sync.rs:2120)
by 0x216F7F: <alloc::sync::Arc<T,A> as core::ops::drop::Drop>::drop (library/alloc/src/sync.rs:2854)
by 0x21467A: core::ptr::drop_in_place<alloc::sync::Arc<ort::environment::Environment>> (library/core/src/ptr/mod.rs:805)
by 0x212885: core::ptr::drop_in_place<core::option::Option<alloc::sync::Arc<ort::environment::Environment>>> (library/core/src/ptr/mod.rs:805)
Address 0x8d476768 is 8 bytes inside a block of size 64 free'd
at 0x484A61D: operator delete(void*, unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x1AFFBA75: __run_exit_handlers (exit.c:108)
by 0x1AFFBBBD: exit (exit.c:138)
by 0x1AFDE1D0: (below main) (libc_start_call_main.h:74)
Block was alloc'd at
at 0x4846FA3: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x21E59678: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x23D82286: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime_providers_cuda.so)
by 0x21E68919: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E6A72B: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x21E6A97C: ??? (in /home/carson/onnxruntime-linux-x64-gpu-1.24.4/lib/libonnxruntime.so)
by 0x22403D: <ort::ep::cuda::CUDA as ort::ep::ExecutionProvider>::register (lib.rs:329)
by 0x21C0BD: ort::ep::apply_execution_providers::register_inner (mod.rs:340)
by 0x21D77E: ort::ep::apply_execution_providers (mod.rs:363)
by 0x2012E5: ort::session::builder::impl_options::<impl ort::session::builder::SessionBuilder>::with_execution_providers (impl_options.rs:39)
by 0x1BE692: gpt2::main (gpt2.rs:14)
by 0x1CAF0A: core::ops::function::FnOnce::call_once (library/core/src/ops/function.rs:250)
On Windows it indeed does not crash. I think during process exit, DLLs can skip cleanups.
I'm starting to feel like this isn't ort's problem to fix. After looking at ONNX Runtime, I realized this is a fundamental issue. CUDA is global to the process. tch uses it through libtorch. ort uses it through ONNX Runtime. Both end up clashing over who owns the CUDA resource. I can't imagine a clean solution that doesn't involve one party giving up shared access or deferring. The alternative is to give users of ort a cleanup() function that can be called to clean up ONNX before main() exits so CUDA is no longer under contention. Not very attractive and defies expectations in Rust.
Are you able to use ort without load-dynamic, so you can use the prebuilt (static) binaries instead? That does fix it. I do agree that this probably isn't directly actionable by ort, but I don't want to leave you with a buggy app either.
Activity
decahedron1 commentedon Mar 30, 2026
No actual
libtorchcode is called in the repro, so I don't think that part is relevant. If the issue disappears after removing it fromCargo.toml, then there might be some weird link-level incompatibility between the two (protobuf?), which isn't fixable byort.Could you also try w/ https://github.com/microsoft/onnxruntime/releases/download/v1.24.4/onnxruntime-linux-x64-gpu-1.24.4.tgz instead of the Python package?
SephVelut commentedon Mar 31, 2026
Same error using onnxruntime-linux-x64-gpu-1.24.4. I pushed that commit to the repro.
I think the problem is with the C atexit handler this crate registers that cleans the global onnx environment on exit.
tch build script does
By just including an enum
Device(0), the compiler links libtorch. One of those dynamically linked libs from libtorch probably have static constructors that also register atexit.tch -> libtorch will sometimes fire first and tear down its CUDA state. Next ort tries to release its onnx runtime environment, which internally uses CUDA, and if CUDA is already gone, then that's heap corruption.
I pushed to the repro a C program that does the exact same onnx runtime and CUDA work as main.rs. But releases the environment explicitly instead of deferring to atexit. No crash, ever.
decahedron1 commentedon Mar 31, 2026
Conveniently, no crash on Windows. It'll be a bit before I can try to repro on Linux; I'm currently sick and the last thing I want to do right now is wrestle with CUDA in WSL2 again 🙃
Torch only sets up its CUDA state once a CUDA operation is used, which isn't the case here. But a deconstructor somewhere is probably to blame.
Did the C test link to libtorch as well? Otherwise it's hard to tell if the bug disappeared because Torch wasn't linked or because of the different environment lifetime.
SephVelut commentedon Mar 31, 2026
I don't blame you 😆
No because I wanted to test onnx runtime in isolation to show it's not to blame. I pushed a new commit that links libtorch.
LD_DEBUG=libs ./c_mainshowslibc10_cuda.solibtorch_cuda.soandlibcudart.so.12. Still no crash.I thought maybe this would lead to an obvious solution but looks a bit more involved if you're not having issues on Windows. I'll have to come back to this issue at a later time if it remains open.
decahedron1 commentedon Apr 2, 2026
Repro'd the crash in WSL2 with CPU Torch, but it disappears under Valgrind.
How helpful.
decahedron1 commentedon Apr 3, 2026
Also disappears with a debug build of ONNX Runtime 🙃🙃🙃
This bit from Valgrind (which I'm not even sure is causing the issue, given there are a million other CUDA-related invalid reads prior too) is all there is to go off of:
Details
SephVelut commentedon Apr 3, 2026
On Windows it indeed does not crash. I think during process exit, DLLs can skip cleanups.
I'm starting to feel like this isn't ort's problem to fix. After looking at ONNX Runtime, I realized this is a fundamental issue. CUDA is global to the process. tch uses it through libtorch. ort uses it through ONNX Runtime. Both end up clashing over who owns the CUDA resource. I can't imagine a clean solution that doesn't involve one party giving up shared access or deferring. The alternative is to give users of ort a cleanup() function that can be called to clean up ONNX before main() exits so CUDA is no longer under contention. Not very attractive and defies expectations in Rust.
decahedron1 commentedon Apr 3, 2026
Are you able to use
ortwithoutload-dynamic, so you can use the prebuilt (static) binaries instead? That does fix it. I do agree that this probably isn't directly actionable byort, but I don't want to leave you with a buggy app either.SephVelut commentedon Apr 3, 2026
I will give that a try. Thank you for taking a look into this, but I believe this issue should be closed now.