From e7778290071c16fd2f6054e88bedbffc950d4725 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Wed, 11 Sep 2024 22:25:42 -0700 Subject: [PATCH] Fixes race condition and limits growth --- Projects/Server/Serialization/BufferWriter.cs | 8 +------- .../Server/Serialization/SerializationThreadWorker.cs | 10 +--------- Projects/Server/World/World.cs | 9 ++------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/Projects/Server/Serialization/BufferWriter.cs b/Projects/Server/Serialization/BufferWriter.cs index 54eb0f834..d3be1f050 100644 --- a/Projects/Server/Serialization/BufferWriter.cs +++ b/Projects/Server/Serialization/BufferWriter.cs @@ -105,13 +105,7 @@ public void Resize(int size) _buffer = newBuffer; } - public virtual void Flush() - { - // Need to avoid buffer.Length = 2, buffer * 2 is 4, but we need 8 or 16bytes, causing an exception. - // The least we need is 16bytes + Index, but we use BufferSize since it should always be big enough for a single - // non-dynamic field. - Resize(Math.Clamp(_buffer.Length * 2, BufferSize, 1024 * 1024 * 64)); - } + public virtual void Flush() => Resize(Math.Clamp(_buffer.Length * 2, BufferSize, _buffer.Length + 1024 * 1024 * 64)); [MethodImpl(MethodImplOptions.AggressiveInlining)] private void FlushIfNeeded(int amount) diff --git a/Projects/Server/Serialization/SerializationThreadWorker.cs b/Projects/Server/Serialization/SerializationThreadWorker.cs index 3d7430b2c..6936603f9 100644 --- a/Projects/Server/Serialization/SerializationThreadWorker.cs +++ b/Projects/Server/Serialization/SerializationThreadWorker.cs @@ -60,15 +60,7 @@ public void Exit() Sleep(); } - // 6GB heap, divide by number of threads from World.GetThreadWorkerCount() - private const ulong totalMemory = 1024 * 1024; - // private static readonly ulong _memoryPerThread = totalMemory / (ulong)World.GetThreadWorkerCount(); - - // private static ulong NextBlockSize(ulong amount) => (amount + 4096UL - 1) & ~(4096UL - 1); - - // public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray((int)NextBlockSize(_memoryPerThread)); - - public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray((int)totalMemory); + public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray(1024 * 1024); // 1MB public void DeallocateHeap() { diff --git a/Projects/Server/World/World.cs b/Projects/Server/World/World.cs index 1b76a5e71..bd4a2f8eb 100644 --- a/Projects/Server/World/World.cs +++ b/Projects/Server/World/World.cs @@ -104,10 +104,7 @@ public static void Configure() } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void WaitForWriteCompletion() - { - _diskWriteHandle.WaitOne(); - } + public static void WaitForWriteCompletion() => _diskWriteHandle.WaitOne(); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void EnqueueForDecay(Item item) @@ -303,7 +300,6 @@ internal static void Snapshot(string snapshotPath) } WaitForWriteCompletion(); // Blocks Save until current disk flush is done. - _diskWriteHandle.Reset(); NetState.FlushAll(); @@ -396,14 +392,13 @@ private static void WriteFiles(object state) // Clear types SerializedTypes.Clear(); + _diskWriteHandle.Set(); Core.LoopContext.Post(FinishWorldSave); } private static void FinishWorldSave() { WorldState = WorldState.Running; - _diskWriteHandle.Set(); - Persistence.PostWorldSaveAll(); // Process decay and safety queues }