diff --git a/Core/NWNXCore.cpp b/Core/NWNXCore.cpp index 0ecab9aa1cb..a5cebd459f7 100644 --- a/Core/NWNXCore.cpp +++ b/Core/NWNXCore.cpp @@ -555,6 +555,9 @@ void NWNXCore::DestroyServerHandler(CAppManager* app) g_core->m_destroyServerHook.reset(); app->DestroyServer(); + + MessageBus::Broadcast("NWNX_CORE_SIGNAL", { "ON_DESTROY_SERVER_AFTER" }); + g_core->Shutdown(); RestoreCrashHandlers(); diff --git a/Plugins/DotNET/DotNETExports.cpp b/Plugins/DotNET/DotNETExports.cpp index 199f9172ee4..b56132a9942 100644 --- a/Plugins/DotNET/DotNETExports.cpp +++ b/Plugins/DotNET/DotNETExports.cpp @@ -137,10 +137,18 @@ static void RegisterHandlers(AllHandlers *handlers, unsigned size) MessageBus::Subscribe("NWNX_CORE_SIGNAL", [](const std::vector& message) { - int spBefore = Utils::PushScriptContext(Constants::OBJECT_INVALID, 0, false); - s_handlers.SignalHandler(message[0].c_str()); - int spAfter = Utils::PopScriptContext(); - ASSERT_MSG(spBefore == spAfter, "spBefore=%x, spAfter=%x", spBefore, spAfter); + // We will crash the server if we try to create a script context after the server is destroyed. + if (message[0] == "ON_DESTROY_SERVER_AFTER") + { + s_handlers.SignalHandler(message[0].c_str()); + } + else + { + int spBefore = Utils::PushScriptContext(Constants::OBJECT_INVALID, 0, false); + s_handlers.SignalHandler(message[0].c_str()); + int spAfter = Utils::PopScriptContext(); + ASSERT_MSG(spBefore == spAfter, "spBefore=%x, spAfter=%x", spBefore, spAfter); + } }); }