Skip to content

Crash on exit with ort + tch + CUDA #564

Closed as not planned
Closed as not planned

Description

@SephVelut

Linux, ort 2.0.0-rc.12, ONNX Runtime 1.23.2, tch 0.23

Using both ort+CUDA and tch will cause a crash when exiting the process.
malloc(): largebin double linked list corrupted (bk)

Repro at https://github.com/SephVelut/ort_tch_min_repro

Activity

  1. decahedron1 commented on Mar 30, 2026

    @decahedron1
    Member

    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.

    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?

  2. SephVelut commented on Mar 31, 2026

    @SephVelut
    Author

    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

    cargo:rustc-link-arg=-Wl,--no-as-needed
    cargo:rustc-link-arg=-ltorch
    

    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.

  3. decahedron1 commented on Mar 31, 2026

    @decahedron1
    Member

    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.

  4. SephVelut commented on Mar 31, 2026

    @SephVelut
    Author

    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.so libtorch_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.

  5. added
    bug
    on Apr 2, 2026
  6. decahedron1 commented on Apr 2, 2026

    @decahedron1
    Member

    Repro'd the crash in WSL2 with CPU Torch, but it disappears under Valgrind.

    Image

    How helpful.

  7. decahedron1 commented on Apr 3, 2026

    @decahedron1
    Member

    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)
    
  8. added
    help wanted
    ep: CUDA
    on Apr 3, 2026
  9. SephVelut commented on Apr 3, 2026

    @SephVelut
    Author

    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.

  10. decahedron1 commented on Apr 3, 2026

    @decahedron1
    Member

    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.

  11. SephVelut commented on Apr 3, 2026

    @SephVelut
    Author

    I will give that a try. Thank you for taking a look into this, but I believe this issue should be closed now.

  12. removed
    help wanted
    on Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugep: CUDAos: linux

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions