mirror of
https://github.com/modernuo/ModernUO
synced 2026-08-11 22:23:06 -04:00
## Problem Running the full `UOContent.Tests` suite, the test host **hangs ~2.5 minutes at shutdown and then crashes** (`Test host process crashed` / run aborted). The tests themselves are fine — they complete in ~1s — but the process can't exit. Captured via `--blame-hang` dump. The blocking thread: ``` System.Threading.WaitHandle.WaitOne() Server.SerializationThreadWorker.Sleep() SerializationThreadWorker.cs:54 (_stopEvent.WaitOne()) Server.SerializationThreadWorker.Exit() SerializationThreadWorker.cs:61 Server.World.ExitSerializationThreads() World.cs:429 Server.Tests.UOContentFixture..ctor() ``` ### Root cause Both collection fixtures (`UOContentFixture` and `PathfindingTestFixture`) each run the **full process-global ModernUO bootstrap**. `World.Load()` is guarded to run once per process, so the **second** fixture's `World.Load()` is a no-op and does **not** respawn the serialization workers — but `World.ExitSerializationThreads()` is **not** guarded, so the second fixture calls `Exit()` on workers whose threads have already terminated. `Exit()` → `Sleep()` → `_stopEvent.WaitOne()` then blocks forever (a dead thread never sets the event). The first collection's tests run; the second collection's fixture deadlocks in its constructor; the host eventually gets killed. This is why single-collection (filtered) runs were fine — only one fixture ever bootstraps — but the full suite hangs. It's not a parallelization race: even strictly sequential, the second fixture deadlocks. ## Fix **(a) Engine — idempotent `SerializationThreadWorker.Exit()`** A second `Exit()` is now a safe no-op instead of a permanent block. Only the owning (main) thread calls `Exit()`, so no synchronization is needed, and the single-call production shutdown path is unchanged. **(b) Tests — one shared bootstrap, strictly sequential collections** - New `TestServerBootstrap.EnsureInitialized()` runs the superset global init **exactly once per process** (lock + once-flag). - `UOContentFixture` / `PathfindingTestFixture` slim down to delegate to it and no longer tear down global state (which the single-bootstrap model owns for the host's lifetime). - `[assembly: CollectionBehavior(DisableTestParallelization = true)]` so collections never overlap. ## Result | | Before | After | |---|---|---| | Tests run (full suite) | 258 (UOContent collection deadlocked) | **418** | | Outcome | 2.5-min hang → host crash | **418 passed, clean exit** | | Wall time | killed | **~7s** |
120 lines
4 KiB
C#
120 lines
4 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2026 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: SerializationThreadWorker.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading;
|
|
|
|
namespace Server;
|
|
|
|
public class SerializationThreadWorker
|
|
{
|
|
private const int MinHeapSize = 1024 * 1024; // 1MB
|
|
private readonly int _index;
|
|
private readonly Thread _thread;
|
|
private readonly AutoResetEvent _startEvent; // Main thread tells the thread to start working
|
|
private readonly AutoResetEvent _stopEvent; // Main thread waits for the worker finish draining
|
|
private bool _pause;
|
|
private bool _exit;
|
|
private bool _exited;
|
|
private byte[] _heap;
|
|
|
|
private readonly ConcurrentQueue<IGenericSerializable> _entities;
|
|
|
|
public SerializationThreadWorker(int index)
|
|
{
|
|
_index = index;
|
|
_startEvent = new AutoResetEvent(false);
|
|
_stopEvent = new AutoResetEvent(false);
|
|
_entities = new ConcurrentQueue<IGenericSerializable>();
|
|
_thread = new Thread(Execute);
|
|
_thread.Start(this);
|
|
}
|
|
|
|
public void Wake()
|
|
{
|
|
_startEvent.Set();
|
|
}
|
|
|
|
public void Sleep()
|
|
{
|
|
Volatile.Write(ref _pause, true);
|
|
_stopEvent.WaitOne();
|
|
}
|
|
|
|
public void Exit()
|
|
{
|
|
if (_exited)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_exited = true;
|
|
_exit = true;
|
|
Wake();
|
|
Sleep();
|
|
}
|
|
|
|
public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray<byte>(MinHeapSize); // 1MB
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void Push(IGenericSerializable entity) => _entities.Enqueue(entity);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ReadOnlySpan<byte> GetHeap(int start, int length) => _heap.AsSpan(start, length);
|
|
|
|
private static void Execute(object obj)
|
|
{
|
|
var worker = (SerializationThreadWorker)obj;
|
|
var threadIndex = (byte)worker._index;
|
|
|
|
var queue = worker._entities;
|
|
var serializedTypes = World.SerializedTypes;
|
|
|
|
while (worker._startEvent.WaitOne())
|
|
{
|
|
var writer = new BufferWriter(worker._heap, true, serializedTypes);
|
|
|
|
while (true)
|
|
{
|
|
var pauseRequested = Volatile.Read(ref worker._pause);
|
|
if (queue.TryDequeue(out var e))
|
|
{
|
|
e.SerializedThread = threadIndex;
|
|
var start = e.SerializedPosition = (int)writer.Position;
|
|
e.Serialize(writer);
|
|
e.SerializedLength = (int)(writer.Position - start);
|
|
}
|
|
else if (pauseRequested) // Break when finished
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
worker._heap = writer.Buffer;
|
|
|
|
writer.Close();
|
|
|
|
worker._stopEvent.Set(); // Allow the main thread to continue now that we are finished
|
|
worker._pause = false;
|
|
|
|
if (Core.Closing || worker._exit)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|