Skip to content

CUDA EP: release_env_on_exit reads freed memory, aborting at exit — reproduces without a second CUDA library (cf. #564, #392, #169) #609

Closed

Description

@jayhesselberth

Summary

release_env_on_exit → ReleaseEnv reads freed memory inside
libonnxruntime_providers_cuda.so, aborting the process at exit with
corrupted double-linked list after a completely successful, correct run.

This is the same failure as #564, #392 and #169. #564 was closed on the theory
that libtorch and ONNX Runtime were clashing over CUDA ownership. I think that
conclusion was premature, and I have a repro that isolates it:

  • no second CUDA framework — no torch, no TensorRT; the only consumer of the
    CUDA EP is ort itself;
  • the probability scales with how much CUDA EP work the process did, which
    matches corrupted double-linked list #392's "increase num_sessions to increase probability" (that reporter
    also had no second framework — TensorRT EP alone);
  • a one-line workaround that prevents ReleaseEnv from running fixes it
    completely, which points squarely at the teardown rather than at any other
    library's destructors.

Results are never affected — every run wrote complete, byte-correct output. Only
the exit status is wrong, which is nonetheless fatal for us: a workflow engine
reads a non-zero exit as a failed job.

Environment

  • ort 2.0.0-rc.13, load-dynamic, cuda
  • onnxruntime 1.27.1 (onnxruntime-linux-x64-gpu_cuda13-1.27.1)
  • CUDA 13.0 driver 580.126.09, NVIDIA A30, Rust 1.9x, glibc 2.34, Rocky 9

Measurement

Two binaries of the same application, interleaved, 10 runs each on identical
input, differing only in how much work goes through the CUDA EP:

build CUDA-EP sessions non-zero exit output complete
before 1 small CNN ([B,1,806] -> [B,2,806]) 0/10 10/10
after that CNN plus 2 LSTM encoder sessions ([B,1,3000] -> [300,B,1280]) 5/10 10/10

Same machine, same input, same onnxruntime. The only change is more CUDA EP
state to unwind. Toggling everything else in our own code made no difference:
our GPU decode off, our zero-copy output binding off, one encoder session instead
of two — all still ~50%.

Valgrind

On a run whose output was complete and correct (10 errors, 5 contexts, all at
teardown, none in the processing loop):

Invalid read of size 8
   at 0x10545A7C: ??? (in .../lib/libonnxruntime_providers_cuda.so)
   by 0xD158780: ??? (in .../lib/libonnxruntime.so.1.27.1)
   by 0xD159013: ??? (in .../lib/libonnxruntime.so.1.27.1)
   by 0xD132ECC: ??? (in .../lib/libonnxruntime.so.1.27.1)
   by 0xD132F9C: ??? (in .../lib/libonnxruntime.so.1.27.1)
   by 0xD10BC9C: ??? (in .../lib/libonnxruntime.so.1.27.1)
   by 0xB59C42: <alloc::sync::Arc<ort::environment::Environment>>::drop_slow (src/lib.rs:327)
   by 0xB59B6F: ort::environment::release_env_on_exit (library/alloc/src/sync.rs:2874)
   by 0x4006282: _dl_fini (dl-fini.c:148)
   by 0x49B9E7C: __run_exit_handlers
   by 0x49B9FCF: exit
Address 0x51f94ee8 is 1,258,744 bytes inside an unallocated block of size 1,360,656 in arena "client"

This is byte-for-byte the same shape as the trace @decahedron1 posted in #564 —
same provider library, same call chain, same "unallocated block". Reaching it
without libtorch in the process is the new information.

gdb on the abort itself (symbolized, .fini_array path):

#5  unlink_chunk.constprop ()
#6  malloc_consolidate ()
#7  _int_free ()
#8  free ()
#9-14  libonnxruntime.so.1.27.1
#15 ort::environment::{impl#5}::drop        ort-2.0.0-rc.13/src/lib.rs:327
#22 ort::environment::release_env_on_exit   environment.rs:85
#23 _dl_fini (dl-fini.c:148)

Also matching #564: the abort disappears under gdb and under valgrind — the
invalid read is still reported, but glibc never gets to the free that notices.

Workaround that works

release_env_on_exit does G_ENV.lock().take(), which only runs ReleaseEnv
when the last Arc<Environment> drops — and every live Session holds one
through SharedSessionInner::_environment. Leaking the objects that own our
sessions keeps the count above zero, so the faulty path never executes:

// after all work is done and all output flushed
std::mem::forget(model);     // owns the encoder Sessions
std::mem::forget(detector);  // owns the CNN Session

Back to 0/10 over the same interleaved 10-run comparison, everything else
unchanged. That it works is itself evidence the problem is confined to
ReleaseEnv and not to some other library's destructor ordering.

--features load-dynamic is not optional for us: the CUDA-enabled
libonnxruntime is supplied at run time on machines that differ from the build
host, so the static-binary suggestion from #564 is not available.

Suggestion

An explicit, supported way to opt out of the at-exit release would turn this from
a mem::forget trick into something documentable — roughly what @SephVelut
proposed at the end of #564. Either direction would do:

  • ort::init().with_release_env_on_exit(false), or
  • an ort::cleanup() the application calls before main returns, with the
    .fini_array hook skipped if it has already run.

The second is nicer in principle but has the same hazard if it is called after
whatever is being freed has gone; the first is a blunt escape hatch that is at
least honest about what it does.

Happy to test a patch — I have a machine that reproduces this at ~50% and a
reduced case if that would help.

Activity

  1. decahedron1 commented on Aug 7, 2026

    @decahedron1
    Member

    😣 Damned if we do, damned if we don't. Not releasing the environment causes other problems like #409.

    I think the only real option at this point would be to make Environment not global, which is gonna be a pain in the ass both for me & for users of ort. A lot of things depend on the environment being 'static right now.

  2. decahedron1 commented on Aug 8, 2026

    @decahedron1
    Member

    Could you give #610 a try?

    [dependencies.ort]
    git = "https://github.com/pykeio/ort"
    rev = "629fde55bd0002886dcb1150f38d6339618c7f00"
    features = [ "cuda", ... ]
  3. added
    bug
    ep: CUDA
    stale
    on Aug 14, 2026
  4. linked a pull request that will close this issueManual `Environment` #610on Aug 19, 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: CUDAstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions