Skip to content

Commit

Permalink
fix: GIL deadlock during the destruction of EndstoneServer
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 8, 2024
1 parent 747e337 commit 26d35af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions include/endstone/detail/singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class Singleton {
return *mInstance;
}

static void reset()
{
mInstance.reset();
}

private:
inline static std::unique_ptr<T> mInstance = nullptr;
};
Expand Down
7 changes: 7 additions & 0 deletions src/endstone_runtime/bedrock/server_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@

#include "bedrock/server/server_instance.h"

#include <pybind11/pybind11.h>

#include "endstone/detail/hook.h"
#include "endstone/detail/plugin/python_plugin_loader.h"
#include "endstone/detail/server.h"
#include "endstone/detail/singleton.h"
#include "endstone/util/color_format.h"

namespace py = pybind11;

using endstone::ColorFormat;
using endstone::detail::EndstoneServer;
using endstone::detail::PythonPluginLoader;
Expand Down Expand Up @@ -54,6 +58,9 @@ void ServerInstanceEventCoordinator::sendServerThreadStarted(ServerInstance &ins

void ServerInstanceEventCoordinator::sendServerThreadStopped(ServerInstance &instance)
{
py::gil_scoped_acquire acquire{};
Singleton<EndstoneServer>::getInstance().disablePlugins();
Singleton<EndstoneServer>::reset(); // we need to explicitly acquire GIL and destroy the server instance as the
// command map and the plugin manager hold shared_ptrs to python objects
ENDSTONE_HOOK_CALL_ORIGINAL(&ServerInstanceEventCoordinator::sendServerThreadStopped, this, instance);
}
16 changes: 10 additions & 6 deletions src/endstone_runtime/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <exception>

#include <pybind11/embed.h>
#include <spdlog/spdlog.h>

#include "endstone/detail/hook.h"
#include "endstone/detail/logger_factory.h"
Expand All @@ -26,21 +25,26 @@
#define ENDSTONE_RUNTIME_CTOR
#endif

[[maybe_unused]] pybind11::scoped_interpreter gInterpreter;
[[maybe_unused]] pybind11::gil_scoped_release gRelease;
namespace py = pybind11;

ENDSTONE_RUNTIME_CTOR int main()
{
auto &logger = endstone::detail::LoggerFactory::getLogger("EndstoneRuntime");
try {
logger.info("Initialising...");
py::initialize_interpreter();
py::module_::import("threading"); // https://github.com/pybind/pybind11/issues/2197
py::gil_scoped_release release{};
release.disarm();

// Install hooks
endstone::detail::hook::install();
return 0;
}
catch (const std::exception &e) {
logger.error("An exception occurred while initialising Endstone runtime.");
logger.error("{}", e.what());
std::terminate();
throw e;
}
}

Expand All @@ -49,7 +53,7 @@ ENDSTONE_RUNTIME_CTOR int main()

[[maybe_unused]] BOOL WINAPI DllMain(_In_ HINSTANCE /*module*/, // handle to DLL module
_In_ DWORD reason, // reason for calling function
_In_ LPVOID /*reserved*/) // reserved
_In_ LPVOID reserved) // reserved
{
switch (reason) {
case DLL_PROCESS_ATTACH: {
Expand All @@ -59,7 +63,7 @@ ENDSTONE_RUNTIME_CTOR int main()
break;
}
case DLL_PROCESS_DETACH: {
std::exit(0); // TODO: without this, the server won't exit on Windows for unknown reason.
break;
}
default:
break;
Expand Down

0 comments on commit 26d35af

Please sign in to comment.