fix(scripting): stabilize mono wrapper test teardown

This commit is contained in:
2026-04-15 14:27:21 +08:00
parent 982a877714
commit 65cb212020
8 changed files with 351 additions and 0 deletions

View File

@@ -29,6 +29,7 @@
#include <mono/metadata/reflection.h>
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <utility>
@@ -40,6 +41,7 @@ namespace {
struct MonoRootState {
MonoDomain* rootDomain = nullptr;
bool initialized = false;
bool cleanupRegistered = false;
};
enum class ManagedComponentKind {
@@ -81,6 +83,19 @@ bool& GetInternalCallRegistrationState() {
return registered;
}
void CleanupMonoRootDomainAtExit() {
MonoRootState& rootState = GetMonoRootState();
if (!rootState.rootDomain) {
return;
}
mono_domain_set(rootState.rootDomain, true);
mono_jit_cleanup(rootState.rootDomain);
rootState.rootDomain = nullptr;
rootState.initialized = false;
GetInternalCallRegistrationState() = false;
}
std::string BuildFullClassName(const std::string& namespaceName, const std::string& className) {
return namespaceName.empty() ? className : namespaceName + "." + className;
}
@@ -2455,6 +2470,10 @@ bool MonoScriptRuntime::InitializeRootDomain() {
mono_domain_set(rootState.rootDomain, true);
RegisterInternalCalls();
rootState.initialized = true;
if (!rootState.cleanupRegistered) {
std::atexit(&CleanupMonoRootDomainAtExit);
rootState.cleanupRegistered = true;
}
return true;
}