fix racing between AutoSave and process exit

This commit is contained in:
cs8425
2026-04-29 19:30:57 +08:00
parent 5f3fc0c746
commit 7e83d2ccd2
2 changed files with 54 additions and 15 deletions

View File

@@ -62,7 +62,7 @@ public class MikuSB
await consoleTask;
ProcessExit(Volatile.Read(ref _exitCode));
await ProcessExit(Volatile.Read(ref _exitCode));
}
#region Exit
@@ -96,11 +96,15 @@ public class MikuSB
_cts.Cancel();
}
private static void ProcessExit(int exitCode)
private static async Task ProcessExit(int exitCode)
{
SocketListener.Connections.Values.ToList().ForEach(x => x.Stop(true));
DatabaseHelper.SaveThread?.Interrupt();
DatabaseHelper.SaveDatabase();
DatabaseHelper.Stop(); // notify stop
await DatabaseHelper.WaitAsync(); // wait AutoSave thread exit
DatabaseHelper.SaveDatabase(); // final flush
Environment.Exit(exitCode);
}