Skip to content

Commit 847fdb7

Browse files
committed
fix: additional lua thread safety and ordering fixes
1 parent f969c33 commit 847fdb7

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

UE4SS/src/Mod/LuaMod.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define NOMINMAX
22

3+
#include <atomic>
34
#include <filesystem>
45
#include <format>
56
#include <limits>
@@ -116,7 +117,7 @@ namespace RC
116117
bool has_return_value{};
117118
// Will be non-nullptr if the UFunction has a return value
118119
Unreal::FProperty* return_property{};
119-
bool scheduled_for_removal{};
120+
std::atomic<bool> scheduled_for_removal{};
120121
};
121122
static std::vector<std::unique_ptr<LuaUnrealScriptFunctionData>> g_hooked_script_function_data{};
122123

@@ -1783,7 +1784,10 @@ No overload found for function 'UnregisterHook'.
17831784
const auto hook_data = std::ranges::find_if(g_hooked_script_function_data, [&](const std::unique_ptr<LuaUnrealScriptFunctionData>& elem) {
17841785
return elem->post_callback_id == post_id && elem->pre_callback_id == pre_id;
17851786
});
1786-
hook_data->get()->scheduled_for_removal = true;
1787+
if (hook_data != g_hooked_script_function_data.end())
1788+
{
1789+
hook_data->get()->scheduled_for_removal = true;
1790+
}
17871791
}
17881792
else
17891793
{
@@ -4259,6 +4263,17 @@ No overload found for function 'FPackageName:IsValidLongPackageName'.
42594263
lua_resetthread(m_main_lua->get_lua_state());
42604264
}
42614265

4266+
// Mark all hooks for this mod as scheduled_for_removal BEFORE closing Lua state
4267+
// This prevents hooks from firing with an invalid Lua state during the window between
4268+
// lua_close and the actual hook unregistration
4269+
for (auto& item : g_hooked_script_function_data)
4270+
{
4271+
if (item->mod == this)
4272+
{
4273+
item->scheduled_for_removal = true;
4274+
}
4275+
}
4276+
42624277
lua_close(lua().get_lua_state());
42634278

42644279
// Unhook all UFunctions for this mod & remove from the map that keeps track of which UFunctions have been hooked

deps/first/Unreal

0 commit comments

Comments
 (0)