Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(onnx): destroy the Ort::Env at exit instead of leaking it

Adeshina Lasisi41fdf4ec1a5a
diff --git a/laya-cpp/include/laya/onnx.hpp b/laya-cpp/include/laya/onnx.hpp
index 64987ab1..428b9393 100644
--- a/laya-cpp/include/laya/onnx.hpp
+++ b/laya-cpp/include/laya/onnx.hpp
@@ -25,8 +25,8 @@ std::string RuntimeVersion();
// A Session over one fused laya.onnx (+ laya.onnx.data beside it). Checks the graph's inputs and
// outputs at once. Run is thread-safe and stops in flight when its CancelToken fires (kCancelled).
// Close is idempotent, refuses new runs, terminates the runs in flight and returns once they have
-// left. A Session or Agent may be held in a static: the process-wide ONNX Runtime environment is
-// never destroyed. No exception leaves this API.
+// left. A Session or Agent may be held in a static: the process-wide ONNX Runtime environment is a
+// static built on the first Open and does not need the session. No exception leaves this API.
Result<std::unique_ptr<infer::Session>> Open(const std::string& graph_path, const Options& options = {});
} // namespace onnx
diff --git a/laya-cpp/src/onnx.cpp b/laya-cpp/src/onnx.cpp
index 94d6b23f..0a47eca6 100644
--- a/laya-cpp/src/onnx.cpp
+++ b/laya-cpp/src/onnx.cpp
@@ -17,13 +17,15 @@ namespace laya {
namespace onnx {
namespace {
-// One Ort::Env per process, leaked on purpose. A session or agent may be held in a static whose
-// destructor runs after any Env destructor would have, and ORT's own statics may already be gone;
-// never destroying the Env means no destruction order can fault. The logging level is FATAL so a
-// refused run is reported only through its Result.
+// One Ort::Env per process, a function-local static. It is built on the first Open, after the
+// runtime's own statics, so it is destroyed before them: leaking it instead (tried first) leaves a
+// live OrtEnv while the runtime's statics die and aborts at exit ("recursive_mutex lock failed",
+// 5 of 6 TSan runs of CloseInterruptsARunInFlight/english, 0 of 4 with this static). A Session or
+// Agent a user holds in a static is destroyed after it, which the static-holder test pins. The
+// logging level is FATAL so a refused run is reported only through its Result.
Ort::Env& SharedEnv() {
- static Ort::Env* const env = new Ort::Env(ORT_LOGGING_LEVEL_FATAL, "laya");
- return *env;
+ static Ort::Env env(ORT_LOGGING_LEVEL_FATAL, "laya");
+ return env;
}
std::string Dtype(ONNXTensorElementDataType t) {
diff --git a/laya-cpp/tests/tools/onnx_static_holder.cpp b/laya-cpp/tests/tools/onnx_static_holder.cpp
index 35decfdc..04ce7e53 100644
--- a/laya-cpp/tests/tools/onnx_static_holder.cpp
+++ b/laya-cpp/tests/tools/onnx_static_holder.cpp
@@ -3,6 +3,8 @@
#include <cstdio>
#include <cstring>
#include <memory>
+#include <thread>
+#include <vector>
#include "laya/onnx.hpp"
@@ -25,6 +27,26 @@ int main(int argc, char** argv) {
return 3;
}
g_agent = std::move(a).value();
+ { // run once so ONNX Runtime's lazily built statics exist, and are built after ours
+ laya::infer::Batch b;
+ b.rows = 1; b.length = 3; b.markers = 3;
+ b.input_ids.assign(3, 1); b.attention_mask.assign(3, 1); b.marker_pos.assign(3, 1);
+ b.marker_mask.assign(3, 1); b.qtype.assign(1, 0);
+ if (!g_session->Run(b, laya::CancelToken()).ok()) return 5;
+ if (!g_agent->Predict(laya::pyvalue::Value::String("hello"), laya::schema::Questions::Parse(
+ R"({"q":{"type":"noul","instructions":"x"}})").value()).ok()) return 6;
+ }
+ { // concurrent runs right before exit leave the runtime's worker threads busy as statics die
+ std::vector<std::thread> ts;
+ for (int i = 0; i < 8; i++) ts.emplace_back([] {
+ laya::infer::Batch b;
+ b.rows = 4; b.length = 64; b.markers = 3;
+ b.input_ids.assign(256, 5); b.attention_mask.assign(256, 1); b.marker_pos.assign(12, 1);
+ b.marker_mask.assign(12, 1); b.qtype.assign(4, 0);
+ for (int k = 0; k < 3; k++) g_session->Run(b, laya::CancelToken());
+ });
+ for (auto& t : ts) t.join();
+ }
if (argc > 3 && !std::strcmp(argv[3], "close")) {
if (!g_session->Close().empty() || !g_agent->Close().empty()) return 4;
}