Skip to content

fix(LuaEngine/Reload): Await callbacks before reloading, to not crash on callback #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/LuaEngine/LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ void Eluna::_ReloadEluna()
LOCK_ELUNA;
ASSERT(IsInitialized());

if (!sEluna->CanReload())
{
sEluna->reloadScheduled = true;
return;
}

if (eConfigMgr->GetOption<bool>("Eluna.PlayerAnnounceReload", false))
eWorldSessionMgr->SendServerMessage(SERVER_MSG_STRING, "Reloading Eluna...");
else
Expand All @@ -147,6 +153,7 @@ void Eluna::_ReloadEluna()
// Run scripts from laoded paths
sEluna->RunScripts();

sEluna->reloadScheduled = false;
reload = false;
}

Expand Down
10 changes: 10 additions & 0 deletions src/LuaEngine/LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ struct LuaScript
class ELUNA_GAME_API Eluna
{
public:
void IncrementCallbacks() { pendingCallbacks++; }
void DecrementCallbacks()
{
pendingCallbacks--;
if (pendingCallbacks == 0 && reloadScheduled)
_ReloadEluna();
}
bool CanReload() const { return pendingCallbacks == 0; }
typedef std::list<LuaScript> ScriptList;

typedef std::recursive_mutex LockType;
Expand All @@ -97,6 +105,8 @@ class ELUNA_GAME_API Eluna
const std::string& GetRequireCPath() const { return lua_requirecpath; }

private:
std::atomic<int> pendingCallbacks{0};
std::atomic<bool> reloadScheduled{false};
static bool reload;
static bool initialized;
static LockType lock;
Expand Down
6 changes: 6 additions & 0 deletions src/LuaEngine/methods/GlobalMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,9 @@ namespace LuaGlobalFunctions
luaL_argerror(L, 2, "unable to make a ref to function");
return 0;
}

// Increment pending callbacks counter
Eluna::GEluna->IncrementCallbacks();

Eluna::GEluna->queryProcessor.AddCallback(db.AsyncQuery(query).WithCallback([L, funcRef](QueryResult result)
{
Expand All @@ -1335,6 +1338,9 @@ namespace LuaGlobalFunctions
Eluna::GEluna->ExecuteCall(1, 0);

luaL_unref(L, LUA_REGISTRYINDEX, funcRef);

// Decrement pending callbacks counter
Eluna::GEluna->DecrementCallbacks();
}));

return 0;
Expand Down