Skip to content

Commit 0e407d7

Browse files
authoredApr 25, 2025
Fix segfault when re-loading an EP library (#24539)
### Description Fixes a segfault that occurs when an EP library is re-loaded in the same process. ### Motivation and Context A recent [PR ](#24430) updated the Environment to unload all EP libraries on destruction of `OrtEnv`. We forgot to properly update the state to mark the EP library as unloaded. Therefore, this caused a segfault when the EP library was re-loaded.
1 parent 16996d9 commit 0e407d7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎onnxruntime/core/session/provider_bridge_ort.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ void ProviderLibrary::Unload() {
17941794
}
17951795
}
17961796

1797+
initialized_ = false;
17971798
handle_ = nullptr;
17981799
provider_ = nullptr;
17991800
}

‎onnxruntime/test/providers/qnn/qnn_basic_test.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ using namespace onnxruntime::logging;
2727

2828
// in test_main.cc
2929
extern std::unique_ptr<Ort::Env> ort_env;
30+
extern "C" void ortenv_setup();
31+
extern "C" void ortenv_teardown();
3032

3133
namespace onnxruntime {
3234
namespace test {
@@ -1232,6 +1234,37 @@ TEST_F(QnnHTPBackendTests, UseHtpSharedMemoryAllocatorForInputs) {
12321234
}
12331235
#endif // BUILD_QNN_EP_STATIC_LIB
12341236

1237+
#if !BUILD_QNN_EP_STATIC_LIB
1238+
// Tests that loading and unloading of an EP library in the same process does not cause a segfault.
1239+
TEST_F(QnnHTPBackendTests, LoadingAndUnloadingOfQnnLibrary_FixSegFault) {
1240+
const ORTCHAR_T* ort_model_path = ORT_MODEL_FOLDER "nhwc_resize_sizes_opset18.quant.onnx";
1241+
1242+
onnxruntime::ProviderOptions options;
1243+
options["backend_type"] = "htp";
1244+
options["offload_graph_io_quantization"] = "0";
1245+
1246+
// This first session will load the QNN EP library for the first time.
1247+
{
1248+
Ort::SessionOptions so;
1249+
so.AppendExecutionProvider("QNN", options);
1250+
1251+
EXPECT_NO_THROW(Ort::Session session(*ort_env, ort_model_path, so));
1252+
}
1253+
1254+
{
1255+
ortenv_teardown(); // Destroy Env to force unloading of EP libraries.
1256+
ortenv_setup();
1257+
1258+
// This next session will reload the QNN EP library.
1259+
// Should not get a segfault.
1260+
Ort::SessionOptions so;
1261+
so.AppendExecutionProvider("QNN", options);
1262+
1263+
EXPECT_NO_THROW(Ort::Session session(*ort_env, ort_model_path, so));
1264+
}
1265+
}
1266+
#endif // !BUILD_QNN_EP_STATIC_LIB
1267+
12351268
#endif // defined(__aarch64__) || defined(_M_ARM64) || defined(__linux__)
12361269
#endif // !defined(ORT_MINIMAL_BUILD)
12371270

0 commit comments

Comments
 (0)