fix: exit save sometimes dropped during shutdown race

This commit is contained in:
itsRevela committed 2026-04-18 10:56:54 -05:00
1 parent 68c3440394
commit 84e4a77409
1 file changed
+24 -10
+24 -10
View File
@@ -756,7 +756,7 @@ int main(int argc, char **argv)
} }
DWORD now = GetTickCount(); DWORD now = GetTickCount();
if ((LONG)(now - nextAutosaveTick) >= 0) if ((LONG)(now - nextAutosaveTick) >= 0 && !IsShutdownRequested() && !app.m_bShutdown)
{ {
if (app.GetXuiServerAction(kServerActionPad) == eXuiServerAction_Idle && !ConsoleSaveFileOriginal::hasPendingBackgroundSave()) if (app.GetXuiServerAction(kServerActionPad) == eXuiServerAction_Idle && !ConsoleSaveFileOriginal::hasPendingBackgroundSave())
{ {
@@ -777,16 +777,30 @@ int main(int argc, char **argv)
LogInfof("shutdown", "Dedicated server stopped"); LogInfof("shutdown", "Dedicated server stopped");
MinecraftServer *server = MinecraftServer::getInstance(); MinecraftServer *server = MinecraftServer::getInstance();
if (server != NULL && !ConsoleSaveFileOriginal::hasPendingBackgroundSave()) if (server != NULL)
{ {
server->setSaveOnExit(true); // Drain any in-flight autosave before requesting the exit save so the
LogWorldIO("requesting save before shutdown"); // async autosave can't overwrite the exit save with an older snapshot,
LogWorldIO("using saveOnExit for shutdown"); // and so m_saveOnExit gets set (prior logic skipped it when a save was
} // pending, causing silent data loss on restart).
if (ConsoleSaveFileOriginal::hasPendingBackgroundSave())
if (ConsoleSaveFileOriginal::hasPendingBackgroundSave()) {
{ LogWorldIO("Draining pending autosave before exit save...");
LogWorldIO("Waiting for autosave to complete..."); const DWORD kDrainTimeoutMs = 30000;
DWORD drainStart = GetTickCount();
while (ConsoleSaveFileOriginal::hasPendingBackgroundSave())
{
if ((LONG)(GetTickCount() - drainStart) > (LONG)kDrainTimeoutMs)
{
LogWorldIO("Autosave drain timed out; continuing with exit save");
break;
}
TickCoreSystems();
Sleep(10);
}
}
server->setSaveOnExit(true);
LogWorldIO("requesting exit save");
} }
MinecraftServer::HaltServer(); MinecraftServer::HaltServer();