From 27598a464a24cdd0a293d7ad6af362777375095f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 15:12:41 +0200 Subject: [PATCH] BaseState: an includeChildren walk keeps its state in locals AddTransition's graph walk kept its visited, leaf and ignore sets in static lists, so two Minions built at once on different threads (zones are built in parallel) walked and cleared each other's state. Soul hit it as "collection was modified" during parallel test runs. The sets are now the call's own. BaseStateTests starts sixteen walks together behind a barrier, fifty rounds, over prebuilt 200-state chains; with the statics restored every run failed (several hundred of 800 walks wrong or throwing); with locals, clean. Co-Authored-By: Claude Opus 5.5 --- .../ServerShared/Agents/States/BaseState.cs | 39 +++++----- tests/Aetheria.Shared.Tests/BaseStateTests.cs | 72 +++++++++++++++++++ 2 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/BaseStateTests.cs diff --git a/Assets/Scripts/ServerShared/Agents/States/BaseState.cs b/Assets/Scripts/ServerShared/Agents/States/BaseState.cs index f3e4c2bcf..98f303094 100644 --- a/Assets/Scripts/ServerShared/Agents/States/BaseState.cs +++ b/Assets/Scripts/ServerShared/Agents/States/BaseState.cs @@ -12,45 +12,40 @@ public class BaseState _agent = agent; } - private static List _lastVisitedStates = new List(); - private static List _leafStates = new List(); - private static List _visitedStates = new List(); - private static HashSet _ignoreStates = new HashSet(); + // The walk's state is the call's own: zones (and so their Minions) are built on more than one thread. public void AddTransition(BaseState targetState, Func condition, Action onTransition = null, bool includeChildren = false, params BaseState[] ignoreStates) { Transitions.Add(new StateTransition(targetState, condition, onTransition)); - + // Traverse the state graph, adding every state that is reachable from this state without traversing over ignored nodes if (includeChildren) { - _visitedStates.Clear(); - _ignoreStates.Clear(); + var visitedStates = new List(); + var ignored = new HashSet(); if(ignoreStates!=null) foreach (var ignoreState in ignoreStates) - _ignoreStates.Add(ignoreState); - _leafStates.Clear(); - _leafStates.Add(this); - _ignoreStates.Add(this); - _ignoreStates.Add(targetState); - while (_leafStates.Count > 0) + ignored.Add(ignoreState); + var leafStates = new List { this }; + ignored.Add(this); + ignored.Add(targetState); + while (leafStates.Count > 0) { - _lastVisitedStates.Clear(); - _lastVisitedStates.AddRange(_leafStates); - _leafStates.Clear(); - foreach (var state in _lastVisitedStates) + var lastVisitedStates = leafStates; + leafStates = new List(); + foreach (var state in lastVisitedStates) { foreach (var transition in state.Transitions) { - if(!_ignoreStates.Contains(transition.TargetState)) + if(!ignored.Contains(transition.TargetState)) { - _visitedStates.Add(transition.TargetState); - _ignoreStates.Add(transition.TargetState); - _leafStates.Add(transition.TargetState); + visitedStates.Add(transition.TargetState); + ignored.Add(transition.TargetState); + leafStates.Add(transition.TargetState); } } } } - foreach (var state in _visitedStates) + foreach (var state in visitedStates) { state.AddTransition(targetState, condition, onTransition); } diff --git a/tests/Aetheria.Shared.Tests/BaseStateTests.cs b/tests/Aetheria.Shared.Tests/BaseStateTests.cs new file mode 100644 index 000000000..34eb409f0 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/BaseStateTests.cs @@ -0,0 +1,72 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using Xunit; + +// A transition added with includeChildren reaches every state its source can reach. Zones, and so their Minions' +// state graphs, are built on more than one thread at once, so each walk must see only its own graph. +public sealed class BaseStateTests +{ + private const int Depth = 200; + + // A chain root -> s1 -> ... -> sN, and the exit a transition from root will lead to. + private static (BaseState root, List chain, BaseState exit) Chain() + { + var root = new BaseState(null); + var chain = new List(); + var previous = root; + for (var i = 0; i < Depth; i++) + { + var state = new BaseState(null); + previous.AddTransition(state, () => false); + chain.Add(state); + previous = state; + } + return (root, chain, new BaseState(null)); + } + + // Every state on the chain gains exactly one transition to the exit, and the exit gains none. + private static bool ExitReachesTheWholeChain((BaseState root, List chain, BaseState exit) graph) => + graph.chain.Prepend(graph.root).All(state => state.Transitions.Count(t => t.TargetState == graph.exit) == 1) + && graph.exit.Transitions.Count == 0; + + private static void AddExit((BaseState root, List chain, BaseState exit) graph) => + graph.root.AddTransition(graph.exit, () => false, null, true, graph.root); + + [Fact] + public void AnIncludeChildrenTransitionReachesTheWholeGraph() + { + var graph = Chain(); + AddExit(graph); + Assert.True(ExitReachesTheWholeChain(graph)); + } + + // Graphs built beforehand, then their walks started together on parallel threads, round after round. + [Fact] + public void GraphWalksOnParallelThreadsSeeOnlyTheirOwnGraph() + { + const int rounds = 50, width = 16; + var wrong = 0; + var threw = 0; + for (var round = 0; round < rounds; round++) + { + var graphs = Enumerable.Range(0, width).Select(_ => Chain()).ToArray(); + using var start = new Barrier(width); + var threads = graphs.Select(graph => new Thread(() => + { + start.SignalAndWait(); + try { AddExit(graph); } + catch (Exception) { Interlocked.Increment(ref threw); } + })).ToArray(); + foreach (var thread in threads) thread.Start(); + foreach (var thread in threads) thread.Join(); + wrong += graphs.Count(graph => !ExitReachesTheWholeChain(graph)); + } + Assert.True(wrong == 0 && threw == 0, $"of {rounds * width} parallel walks, {wrong} left the wrong graph and {threw} threw"); + } +}