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): opt out of runtime telemetry before the first Env

Adeshina Lasisi94cbc5a7d294
diff --git a/laya-cpp/include/laya/onnx.hpp b/laya-cpp/include/laya/onnx.hpp
index c65b80fd..d2deafe8 100644
--- a/laya-cpp/include/laya/onnx.hpp
+++ b/laya-cpp/include/laya/onnx.hpp
@@ -25,9 +25,9 @@ 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 held in a static should be Closed before exit: the process-wide ONNX
-// Runtime environment is a static built on the first Open, and the runtime's own statics may be gone
-// by static destruction. Close is cheap and idempotent. No exception leaves this API.
+// left. A Session or Agent may be held in a static, closed or not. No exception leaves this API.
+// The first Open sets ORT_DISABLE_TELEMETRY=1 unless it is already set: the runtime's telemetry
+// thread can abort the process at exit. A program that builds its own OrtEnv first must set it too.
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 0a47eca6..2d04f0c7 100644
--- a/laya-cpp/src/onnx.cpp
+++ b/laya-cpp/src/onnx.cpp
@@ -6,6 +6,7 @@
#include <atomic>
#include <chrono>
#include <condition_variable>
+#include <cstdlib>
#include <memory>
#include <mutex>
#include <set>
@@ -17,14 +18,23 @@ namespace laya {
namespace onnx {
namespace {
-// 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.
+// ONNX Runtime 1.30 on POSIX starts a telemetry uploader thread with its first OrtEnv unless
+// ORT_DISABLE_TELEMETRY is truthy then. At exit that thread can lock a mutex the runtime's statics
+// already destroyed, which aborts the process ("recursive_mutex lock failed"). The variable is set
+// before our Env is built; a value the user already set wins.
+bool DisableRuntimeTelemetry() {
+#ifndef _WIN32
+ setenv("ORT_DISABLE_TELEMETRY", "1", 0);
+#endif
+ return true;
+}
+
+// One Ort::Env per process, built on the first Open. The logging level is FATAL so a refused run is
+// reported only through its Result.
Ort::Env& SharedEnv() {
+ static const bool telemetry_off = DisableRuntimeTelemetry();
static Ort::Env env(ORT_LOGGING_LEVEL_FATAL, "laya");
+ (void)telemetry_off;
return env;
}
diff --git a/laya-cpp/tests/onnx_test.cpp b/laya-cpp/tests/onnx_test.cpp
index 6c672d38..6b8de1f8 100644
--- a/laya-cpp/tests/onnx_test.cpp
+++ b/laya-cpp/tests/onnx_test.cpp
@@ -7,16 +7,22 @@
#include <chrono>
#include <cmath>
#include <cstdio>
+#include <cstring>
#include <filesystem>
#include <fstream>
#include <limits>
#include <map>
#include <mutex>
+#include <set>
+#include <string>
#include <thread>
+#include <vector>
#include <sys/wait.h>
#include <unistd.h>
+extern char** environ;
+
#include "golden.hpp"
#include "laya/onnx.hpp"
#include "laya/onnx_graph.hpp"
@@ -781,13 +787,25 @@ TEST_P(OnnxRig, CloseReturnsPromptlyUnderSustainedRunTraffic) {
TEST_P(OnnxRig, AStaticSessionAndAgentExitCleanly) {
const Rig& rig = kRigs[GetParam()];
NEED_RIG(rig, graph, dir);
+ // The child gets no variable that turns the runtime's telemetry off, so it exits with the uploader
+ // thread running unless Open opts out (the abort this test guards against).
+ const std::set<std::string> drop = {"ORT_DISABLE_TELEMETRY", "ORT_RUNNING_UNIT_TESTS", "CI", "TF_BUILD",
+ "GITHUB_ACTIONS", "GITLAB_CI", "CIRCLECI", "TRAVIS", "JENKINS_URL",
+ "CODEBUILD_BUILD_ID", "BUILDKITE", "TEAMCITY_VERSION", "APPVEYOR",
+ "BITBUCKET_BUILD_NUMBER", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"};
+ std::vector<const char*> envp;
+ for (char** e = environ; *e != nullptr; e++) {
+ const char* eq = std::strchr(*e, '=');
+ if (eq == nullptr || drop.count(std::string(*e, eq - *e)) == 0) envp.push_back(*e);
+ }
+ envp.push_back(nullptr);
for (const char* mode : {"", "close"}) {
const pid_t pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
const char* argv[] = {LAYA_STATIC_HOLDER, graph.c_str(), dir.c_str(), mode, nullptr};
if (*mode == 0) argv[3] = nullptr;
- execv(LAYA_STATIC_HOLDER, const_cast<char* const*>(argv));
+ execve(LAYA_STATIC_HOLDER, const_cast<char* const*>(argv), const_cast<char* const*>(envp.data()));
_exit(127);
}
int status = 0;
diff --git a/laya-cpp/tests/tools/onnx_static_holder.cpp b/laya-cpp/tests/tools/onnx_static_holder.cpp
index 04ce7e53..efd7356c 100644
--- a/laya-cpp/tests/tools/onnx_static_holder.cpp
+++ b/laya-cpp/tests/tools/onnx_static_holder.cpp
@@ -1,6 +1,7 @@
// Holds a Session and an Agent in statics, as a user program may, then exits normally. Exit code 0
// means static destruction did not fault. argv: graph checkpoint_dir [close].
#include <cstdio>
+#include <cstdlib>
#include <cstring>
#include <memory>
#include <thread>
@@ -21,6 +22,7 @@ int main(int argc, char** argv) {
return 2;
}
g_session = std::move(s).value();
+ if (std::getenv("ORT_DISABLE_TELEMETRY") == nullptr) return 7; // Open must opt out first
auto a = laya::agent::OpenOnnx(argv[2], argv[1]);
if (!a.ok()) {
std::fprintf(stderr, "agent: %s\n", a.error().c_str());