Skip to content

Commit 3e93403

Browse files
authored
nodejs: Release Ort Env before main function returns (microsoft#17288)
### Description Release OrtEnv before main function returns. Before this change, OrtEnv is deleted when C/C++ runtime destructs all global variables in ONNX Runtime's core framework. The callstack is like this: ``` * frame #0: 0x00007fffee39f5a6 libonnxruntime.so.1.16.0`onnxruntime::Environment::~Environment(this=0x00007fffee39fbf2) at environment.h:20:7 frame #1: 0x00007fffee39f614 libonnxruntime.so.1.16.0`std::default_delete<onnxruntime::Environment>::operator()(this=0x00007ffff4c30e50, __ptr=0x0000000005404b00) const at unique_ptr.h:85:2 frame #2: 0x00007fffee39edca libonnxruntime.so.1.16.0`std::unique_ptr<onnxruntime::Environment, std::default_delete<onnxruntime::Environment>>::~unique_ptr(this=0x5404b00) at unique_ptr.h:361:17 frame #3: 0x00007fffee39e2ab libonnxruntime.so.1.16.0`OrtEnv::~OrtEnv(this=0x00007ffff4c30e50) at ort_env.cc:43:1 frame #4: 0x00007fffee39fa96 libonnxruntime.so.1.16.0`std::default_delete<OrtEnv>::operator()(this=0x00007fffefff8f78, __ptr=0x00007ffff4c30e50) const at unique_ptr.h:85:2 frame #5: 0x00007fffee39f394 libonnxruntime.so.1.16.0`std::unique_ptr<OrtEnv, std::default_delete<OrtEnv>>::~unique_ptr(this=0x7ffff4c30e50) at unique_ptr.h:361:17 frame #6: 0x00007ffff78574b5 libc.so.6`__run_exit_handlers + 261 frame #7: 0x00007ffff7857630 libc.so.6`exit + 32 frame #8: 0x00007ffff783feb7 libc.so.6`__libc_start_call_main + 135 frame #9: 0x00007ffff783ff60 libc.so.6`__libc_start_main@@GLIBC_2.34 + 128 frame #10: 0x0000000000abbdee node`_start + 46 ``` After this change, OrtEnv will be deleted before the main function returns and nodejs is still alive.
1 parent 93ae17d commit 3e93403

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

js/node/script/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const args = [
4141
'cmake-js',
4242
(REBUILD ? 'reconfigure' : 'configure'),
4343
`--arch=${ARCH}`,
44-
'--CDnapi_build_version=3',
44+
'--CDnapi_build_version=6',
4545
`--CDCMAKE_BUILD_TYPE=${CONFIG}`,
4646
];
4747
if (ONNXRUNTIME_BUILD_DIR && typeof ONNXRUNTIME_BUILD_DIR === 'string') {

js/node/src/inference_session_wrap.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
#include "tensor_helper.h"
1111

1212
Napi::FunctionReference InferenceSessionWrap::constructor;
13-
Ort::Env *InferenceSessionWrap::ortEnv;
1413

1514
Napi::Object InferenceSessionWrap::Init(Napi::Env env, Napi::Object exports) {
1615
// create ONNX runtime env
1716
Ort::InitApi();
18-
ortEnv = new Ort::Env{ORT_LOGGING_LEVEL_WARNING, "onnxruntime-node"};
19-
17+
ORT_NAPI_THROW_ERROR_IF(
18+
Ort::Global<void>::api_ == nullptr, env,
19+
"Failed to initialize ONNX Runtime API. It could happen when this nodejs binding was built with a higher version "
20+
"ONNX Runtime but now runs with a lower version ONNX Runtime DLL(or shared library).");
21+
auto ortEnv = new Ort::Env{ORT_LOGGING_LEVEL_WARNING, "onnxruntime-node"};
22+
env.SetInstanceData(ortEnv);
2023
// initialize binding
2124
Napi::HandleScope scope(env);
2225

@@ -28,7 +31,6 @@ Napi::Object InferenceSessionWrap::Init(Napi::Env env, Napi::Object exports) {
2831

2932
constructor = Napi::Persistent(func);
3033
constructor.SuppressDestruct();
31-
3234
exports.Set("InferenceSession", func);
3335
return exports;
3436
}
@@ -54,7 +56,7 @@ Napi::Value InferenceSessionWrap::LoadModel(const Napi::CallbackInfo &info) {
5456
Napi::String value = info[0].As<Napi::String>();
5557

5658
ParseSessionOptions(info[1].As<Napi::Object>(), sessionOptions);
57-
this->session_.reset(new Ort::Session(OrtEnv(),
59+
this->session_.reset(new Ort::Session(*env.GetInstanceData<Ort::Env>(),
5860
#ifdef _WIN32
5961
reinterpret_cast<const wchar_t *>(value.Utf16Value().c_str()),
6062
#else
@@ -69,8 +71,9 @@ Napi::Value InferenceSessionWrap::LoadModel(const Napi::CallbackInfo &info) {
6971
int64_t bytesLength = info[2].As<Napi::Number>().Int64Value();
7072

7173
ParseSessionOptions(info[1].As<Napi::Object>(), sessionOptions);
72-
this->session_.reset(
73-
new Ort::Session(OrtEnv(), reinterpret_cast<char *>(buffer) + bytesOffset, bytesLength, sessionOptions));
74+
this->session_.reset(new Ort::Session(*env.GetInstanceData<Ort::Env>(),
75+
reinterpret_cast<char *>(buffer) + bytesOffset, bytesLength,
76+
sessionOptions));
7477
} else {
7578
ORT_NAPI_THROW_TYPEERROR(
7679
env,

js/node/src/inference_session_wrap.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ class InferenceSessionWrap : public Napi::ObjectWrap<InferenceSessionWrap> {
5454
// persistent constructor
5555
static Napi::FunctionReference constructor;
5656

57-
// global env
58-
static Ort::Env *ortEnv;
59-
static Ort::Env &OrtEnv() { return *ortEnv; }
60-
6157
// session objects
6258
bool initialized_;
6359
std::unique_ptr<Ort::Session> session_;

0 commit comments

Comments
 (0)