From d68b9c20d69e037cff60260fe2e1fb198c62bba4 Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Thu, 16 Jan 2020 17:11:13 +0100 Subject: [PATCH] Mono/C#: Script interface calls now attach the current thread Added guards to all C# script interface calls to attach the current thread for the current scope if the thread is not already attached. This is far from ideal, as attaching the thread is not cheap and all managed thread local storage is lost when we detach the thread at the end of the calls. However, it's the best we can do for now to avoid crashing when an unattached thread tries to interact with C# code. --- modules/mono/csharp_script.cpp | 112 +++++++++++++++++-------- modules/mono/mono_gd/gd_mono_utils.cpp | 35 +++++++- modules/mono/mono_gd/gd_mono_utils.h | 23 ++++- 3 files changed, 128 insertions(+), 42 deletions(-) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 8a024eef131..c68404fdf51 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -559,6 +559,7 @@ Vector CSharpLanguage::debug_get_current_stack_info() #ifdef DEBUG_ENABLED _TLS_RECURSION_GUARD_V_(Vector()); + GD_MONO_SCOPE_THREAD_ATTACH; if (!gdmono->is_runtime_initialized() || !GDMono::get_singleton()->get_core_api_assembly() || !GDMonoCache::cached_data.corlib_cache_updated) return Vector(); @@ -583,6 +584,7 @@ Vector CSharpLanguage::debug_get_current_stack_info() Vector CSharpLanguage::stack_trace_get_info(MonoObject *p_stack_trace) { _TLS_RECURSION_GUARD_V_(Vector()); + GD_MONO_SCOPE_THREAD_ATTACH; MonoException *exc = NULL; @@ -689,6 +691,7 @@ void CSharpLanguage::reload_all_scripts() { #ifdef GD_MONO_HOT_RELOAD if (is_assembly_reloading_needed()) { + GD_MONO_SCOPE_THREAD_ATTACH; reload_assemblies(false); } #endif @@ -706,6 +709,7 @@ void CSharpLanguage::reload_tool_script(const Ref