Fix segfault importing onnxruntime from static init order fiasco - #29880
Merged
Conversation
tianleiwu
enabled auto-merge (squash)
July 26, 2026 02:29
bmehta001
approved these changes
Jul 27, 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.
Description
import onnxruntimesegfaults duringdlopenofonnxruntime_pybind11_state.soon Linux. This removes the global initializer inonnxruntime_pybind_state.ccthat eagerly callsEnv::Default(), and resolves the platformEnvon first use at its two call sites instead.Motivation and Context
The module has a namespace-scope dynamic initializer:
static Env& platform_env = Env::Default();Since POSIX telemetry landed (#27379),
Env::Default()constructsPosixEnv, whosePosixTelemetrymember initializes the 1DS SDK in its constructor. That path readsdefaultRuntimeConfig, a namespace-scopestatic ILogConfigurationdefined in the 1DS SDK'sRuntimeConfig_Default.hpp, which lives in a different translation unit of the same shared library.Dynamic initialization order across translation units is unspecified, and the pybind TU's initializer runs first.
Variant::merge_maptherefore iterates a still zero-initializedstd::map:_M_node_count == 0, but_M_header._M_leftisnullptrrather than self-pointing, sobegin() != end()and the loop dereferences null.Textbook static initialization order fiasco. Backtrace (Release build relinked without
--strip-allto recover symbols):The crash is independent of
LD_LIBRARY_PATHandCUDA_VISIBLE_DEVICES— it happens before any ORT runtime code runs.libonnxruntime.sois unaffected because it contains no global initializer that reachesEnv::Default(), which is why C/C++ and onnxruntime-genai consumers do not see it.Both remaining uses of
platform_envare inside pybind lambdas that run long after load, so callingEnv::Default()there is safe. This also removes the now-staleTODO: we may delay-init this variableand a pre-existing#pragma warning(push)that should have beenpop.Note for follow-up:
Env::Default()is now unsafe to call from any dynamic initializer. This was the only such call site in the tree, but hardeningPosixTelemetryto defer SDK initialization out of its constructor would remove the hazard entirely.Tests
Verified on a Linux CUDA 13 Release build (
onnxruntime_USE_TELEMETRYenabled):dlopenofonnxruntime_pybind11_state.sosucceeds (previously SIGSEGV).import onnxruntimereports the version and['CUDAExecutionProvider', 'CPUExecutionProvider'].onnxruntime.enable_telemetry_events()/disable_telemetry_events()— the two call sites changed here — work.LD_LIBRARY_PATHunset andCUDA_VISIBLE_DEVICESempty.