From aa6692426d415912e43f65610d09536d22b0266b Mon Sep 17 00:00:00 2001 From: kuwacom Date: Sun, 8 Mar 2026 22:27:14 +0900 Subject: [PATCH] fix: replace shutdown flag with atomic variable for thread safety --- Minecraft.Server/Windows64/ServerMain.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Minecraft.Server/Windows64/ServerMain.cpp b/Minecraft.Server/Windows64/ServerMain.cpp index d6c8e32f9..638358042 100644 --- a/Minecraft.Server/Windows64/ServerMain.cpp +++ b/Minecraft.Server/Windows64/ServerMain.cpp @@ -32,6 +32,8 @@ #include #include +#include + extern ATOM MyRegisterClass(HINSTANCE hInstance); extern BOOL InitInstance(HINSTANCE hInstance, int nCmdShow); extern HRESULT InitDevice(); @@ -65,16 +67,20 @@ struct DedicatedServerConfig bool showHelp; }; -static volatile bool g_shutdownRequested = false; +static std::atomic g_shutdownRequested(false); static const DWORD kDefaultAutosaveIntervalMs = 60 * 1000; static const int kServerActionPad = 0; +static bool IsShutdownRequested() +{ + return g_shutdownRequested.load(); +} + namespace ServerRuntime { void RequestDedicatedServerShutdown() { - g_shutdownRequested = true; - app.m_bShutdown = true; + g_shutdownRequested.store(true); } } @@ -552,7 +558,7 @@ int main(int argc, char **argv) C4JThread *startThread = new C4JThread(&CGameNetworkManager::RunNetworkGameThreadProc, (LPVOID)param, "RunNetworkGame"); startThread->Run(); - while (startThread->isRunning() && !g_shutdownRequested) + while (startThread->isRunning() && !IsShutdownRequested()) { TickCoreSystems(); Sleep(10); @@ -574,7 +580,7 @@ int main(int argc, char **argv) LogStartupStep("server startup complete"); LogInfof("startup", "Dedicated server listening on %s:%d", g_Win64MultiplayerIP, g_Win64MultiplayerPort); - if (worldBootstrap.status == eWorldBootstrap_CreatedNew && !g_shutdownRequested && !app.m_bShutdown) + if (worldBootstrap.status == eWorldBootstrap_CreatedNew && !IsShutdownRequested() && !app.m_bShutdown) { // Windows64 suppresses saveToDisc right after new world creation // Dedicated Server explicitly runs the initial save here @@ -601,13 +607,13 @@ int main(int argc, char **argv) ServerRuntime::ServerCli serverCli; serverCli.Start(); - while (!g_shutdownRequested && !app.m_bShutdown) + while (!IsShutdownRequested() && !app.m_bShutdown) { TickCoreSystems(); HandleXuiActions(); serverCli.Poll(); - if (g_shutdownRequested || app.m_bShutdown) + if (IsShutdownRequested() || app.m_bShutdown) { break; } @@ -638,6 +644,7 @@ int main(int argc, char **argv) Sleep(10); } serverCli.Stop(); + app.m_bShutdown = true; LogInfof("shutdown", "Dedicated server stopped"); MinecraftServer *server = MinecraftServer::getInstance();