Fix concurrent access issues with ConnectedClients

Replaced direct access to GameMain.Server.ConnectedClients with array snapshots in multiple server-side classes to prevent concurrent modification issues during parallel updates. Also updated PhysicsBody and LevelTrigger to avoid static/shared state in parallel contexts, improving thread safety and reliability.
This commit is contained in:
Eero
2026-01-08 00:26:29 +08:00
parent f4a0d149ca
commit caec44c57d
13 changed files with 108 additions and 59 deletions

View File

@@ -28,11 +28,14 @@ namespace Barotrauma
}
}
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
if (GameMain.Server is { ServerSettings.RespawnMode: RespawnMode.Permadeath } &&
GameMain.GameSession?.Campaign is MultiPlayerCampaign mpCampaign &&
causeOfDeath != CauseOfDeathType.Disconnected)
{
Client ownerClient = GameMain.Server.ConnectedClients.FirstOrDefault(c => c.Character == this);
Client ownerClient = clients.FirstOrDefault(c => c.Character == this);
if (ownerClient != null)
{
ownerClient.SpectateOnly = true;
@@ -51,7 +54,7 @@ namespace Barotrauma
if (HasAbilityFlag(AbilityFlags.RetainExperienceForNewCharacter))
{
var ownerClient = GameMain.Server.ConnectedClients.Find(c => c.Character == this);
var ownerClient = clients.FirstOrDefault(c => c.Character == this);
if (ownerClient != null)
{
(GameMain.GameSession?.GameMode as MultiPlayerCampaign)?.SaveExperiencePoints(ownerClient);
@@ -62,7 +65,7 @@ namespace Barotrauma
if (CauseOfDeath.Killer != null && CauseOfDeath.Killer.IsTraitor && CauseOfDeath.Killer != this)
{
var owner = GameMain.Server.ConnectedClients.Find(c => c.Character == this);
var owner = clients.FirstOrDefault(c => c.Character == this);
if (owner != null)
{
if (!GameMain.LuaCs.Game.overrideTraitors)
@@ -71,7 +74,7 @@ namespace Barotrauma
}
}
}
foreach (Client client in GameMain.Server.ConnectedClients)
foreach (Client client in clients)
{
if (client.InGame)
{

View File

@@ -490,7 +490,9 @@ namespace Barotrauma
case ControlEventData controlEventData:
Client owner = controlEventData.Owner;
msg.WriteBoolean(owner == c && owner.Character == this);
msg.WriteByte(owner != null && owner.Character == this && GameMain.Server.ConnectedClients.Contains(owner) ? owner.SessionId : (byte)0);
// Create snapshot to avoid concurrent access issues during parallel updates
var connectedClients = GameMain.Server.ConnectedClients.ToArray();
msg.WriteByte(owner != null && owner.Character == this && connectedClients.Contains(owner) ? owner.SessionId : (byte)0);
msg.WriteBoolean(info is { RenamingEnabled: true });
break;
case CharacterStatusEventData statusEventData:
@@ -746,7 +748,9 @@ namespace Barotrauma
return;
}
Client ownerClient = GameMain.Server.ConnectedClients.Find(c => c.Character == this && (!c.SpectateOnly || !GameMain.Server.ServerSettings.AllowSpectating));
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
Client ownerClient = clients.FirstOrDefault(c => c.Character == this && (!c.SpectateOnly || !GameMain.Server.ServerSettings.AllowSpectating));
if (ownerClient != null)
{
msg.WriteBoolean(true);

View File

@@ -82,10 +82,13 @@ namespace Barotrauma
private bool IsBlockedByAnotherConversation(IEnumerable<Entity> targets, float duration)
{
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
if (targets == null || targets.None())
{
//if the action doesn't target anyone in specific, it's shown to every client
foreach (var client in GameMain.Server.ConnectedClients)
foreach (var client in clients)
{
if (IsBlockedByAnotherConversation(client, duration)) { return true; }
}
@@ -95,7 +98,7 @@ namespace Barotrauma
foreach (Entity e in targets)
{
if (e is not Character character || !character.IsRemotePlayer) { continue; }
Client targetClient = GameMain.Server.ConnectedClients.Find(c => c.Character == character);
Client targetClient = clients.FirstOrDefault(c => c.Character == character);
if (targetClient != null && IsBlockedByAnotherConversation(targetClient, duration)) { return true; }
}
}
@@ -117,13 +120,16 @@ namespace Barotrauma
partial void ShowDialog(Character speaker, Character targetCharacter)
{
targetClients.Clear();
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
if (!TargetTag.IsEmpty)
{
IEnumerable<Entity> entities = ParentEvent.GetTargets(TargetTag);
foreach (Entity e in entities)
{
if (e is not Character character || !character.IsRemotePlayer) { continue; }
Client targetClient = GameMain.Server.ConnectedClients.Find(c => c.Character == character);
Client targetClient = clients.FirstOrDefault(c => c.Character == character);
if (targetClient != null)
{
targetClients.Add(targetClient);
@@ -135,7 +141,7 @@ namespace Barotrauma
}
else
{
foreach (Client c in GameMain.Server.ConnectedClients)
foreach (Client c in clients)
{
if (CanClientReceive(c))
{

View File

@@ -9,48 +9,52 @@ namespace Barotrauma;
partial class EventLogAction : EventAction
{
partial void AddEntryProjSpecific(EventLog? eventLog, string displayText)
partial void AddEntryProjSpecific(EventLog? eventLog, string displayText)
{
if (eventLog == null) { return; }
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
if (!TargetTag.IsEmpty)
{
if (eventLog == null) { return; }
if (!TargetTag.IsEmpty)
List<Client> targetClients = new List<Client>();
foreach (var target in ParentEvent.GetTargets(TargetTag))
{
List<Client> targetClients = new List<Client>();
foreach (var target in ParentEvent.GetTargets(TargetTag))
if (target is Character character)
{
if (target is Character character)
var ownerClient = clients.FirstOrDefault(c => c.Character == character);
if (ownerClient != null)
{
var ownerClient = GameMain.Server.ConnectedClients.Find(c => c.Character == character);
if (ownerClient != null)
{
targetClients.Add(ownerClient);
}
}
else
{
DebugConsole.AddWarning($"{target} is not a valid target for an EventLogAction. The target should be a character.",
ParentEvent.Prefab.ContentPackage);
targetClients.Add(ownerClient);
}
}
if (eventLog!.TryAddEntry(ParentEvent.Prefab.Identifier, Id, displayText, targetClients) && ShowInServerLog)
else
{
Log(targetClients);
DebugConsole.AddWarning($"{target} is not a valid target for an EventLogAction. The target should be a character.",
ParentEvent.Prefab.ContentPackage);
}
}
else
if (eventLog!.TryAddEntry(ParentEvent.Prefab.Identifier, Id, displayText, targetClients) && ShowInServerLog)
{
if (eventLog.TryAddEntry(ParentEvent.Prefab.Identifier, Id, displayText, GameMain.Server.ConnectedClients) && ShowInServerLog)
{
Log(targetClients: null);
}
}
void Log(List<Client>? targetClients)
{
string clientStr = targetClients == null || targetClients.None() ?
string.Empty :
$" ({string.Join(", ", targetClients.Select(c => NetworkMember.ClientLogName(c)))})";
GameServer.Log($"Event \"{ParentEvent.Prefab.Name}\"{clientStr}: " + displayText,
ParentEvent is TraitorEvent ? ServerLog.MessageType.Traitors : ServerLog.MessageType.Chat);
Log(targetClients);
}
}
else
{
if (eventLog.TryAddEntry(ParentEvent.Prefab.Identifier, Id, displayText, clients) && ShowInServerLog)
{
Log(targetClients: null);
}
}
void Log(List<Client>? targetClients)
{
string clientStr = targetClients == null || targetClients.None() ?
string.Empty :
$" ({string.Join(", ", targetClients.Select(c => NetworkMember.ClientLogName(c)))})";
GameServer.Log($"Event \"{ParentEvent.Prefab.Name}\"{clientStr}: " + displayText,
ParentEvent is TraitorEvent ? ServerLog.MessageType.Traitors : ServerLog.MessageType.Chat);
}
}
}

View File

@@ -1,3 +1,5 @@
using System.Linq;
namespace Barotrauma
{
partial class EventObjectiveAction : EventAction
@@ -13,9 +15,12 @@ namespace Barotrauma
ParentObjectiveId,
CanBeCompleted);
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
if (TargetTag.IsEmpty)
{
foreach (var client in GameMain.Server.ConnectedClients)
foreach (var client in clients)
{
if (client.Character == null) { continue; }
EventManager.ServerWriteObjective(client, objective);
@@ -26,11 +31,11 @@ namespace Barotrauma
foreach (var target in ParentEvent.GetTargets(TargetTag))
{
if (target is not Character character) { continue; }
var ownerClient = GameMain.Server.ConnectedClients.Find(c => c.Character == character);
var ownerClient = clients.FirstOrDefault(c => c.Character == character);
if (ownerClient == null) { continue; }
EventManager.ServerWriteObjective(ownerClient, objective);
}
}
}
}
}
}

View File

@@ -14,8 +14,10 @@ partial class HighlightAction : EventAction
IEnumerable<Client>? targetClients = null;
if (targetCharacters != null)
{
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
targetClients = targetCharacters
.Select(c => GameMain.Server.ConnectedClients.FirstOrDefault(client => client.Character == c))
.Select(c => clients.FirstOrDefault(client => client.Character == c))
.Where(c => c != null)!;
}
GameMain.Server?.CreateEntityEvent(item, new Item.SetHighlightEventData(State, highlightColor, targetClients));

View File

@@ -1,5 +1,6 @@
using Barotrauma.Networking;
using Barotrauma.Networking;
using System.Collections.Generic;
using System.Linq;
namespace Barotrauma
{
@@ -22,7 +23,9 @@ namespace Barotrauma
private static void NotifyMissionUnlock(Mission mission)
{
foreach (Client client in GameMain.Server.ConnectedClients)
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
foreach (Client client in clients)
{
NotifyMissionUnlock(mission, client);
}
@@ -40,4 +43,4 @@ namespace Barotrauma
GameMain.Server.ServerPeer.Send(outmsg, client.Connection, DeliveryMethod.Reliable);
}
}
}
}

View File

@@ -25,4 +25,4 @@ namespace Barotrauma
}
}
}
}
}

View File

@@ -76,7 +76,9 @@ namespace Barotrauma.Items.Components
{
var (msg, deliveryMethod) = PrepareToSend(opcode, data);
foreach (Client client in GameMain.Server.ConnectedClients)
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
foreach (Client client in clients)
{
if (predicate is not null && !predicate(client)) { continue; }
@@ -391,4 +393,4 @@ namespace Barotrauma.Items.Components
CreateServerEvent(new CircuitBoxServerUpdateSelection(nodes, wires, ios, labels));
}
}
}
}

View File

@@ -29,7 +29,9 @@ namespace Barotrauma
//don't create updates if all clients are very far from the hull
float hullUpdateDistanceSqr = NetConfig.HullUpdateDistance * NetConfig.HullUpdateDistance;
if (!GameMain.Server.ConnectedClients.Any(c =>
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
if (!clients.Any(c =>
(c.Character != null && Vector2.DistanceSquared(c.Character.WorldPosition, WorldPosition) < hullUpdateDistanceSqr) ||
(c.SpectatePos != null && Vector2.DistanceSquared(c.SpectatePos.Value, WorldPosition) < hullUpdateDistanceSqr)) )
{

View File

@@ -4799,7 +4799,9 @@ namespace Barotrauma.Networking
public static string CharacterLogName(Character character)
{
if (character == null) { return "[NULL]"; }
Client client = GameMain.Server.ConnectedClients.Find(c => c.Character == character);
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
Client client = clients.FirstOrDefault(c => c.Character == character);
return ClientLogName(client, character.LogName);
}
@@ -4810,8 +4812,8 @@ namespace Barotrauma.Networking
GameMain.LuaCs?.Hook?.Call("serverLog", line, messageType);
GameMain.Server.ServerSettings.ServerLog.WriteLine(line, messageType);
foreach (Client client in GameMain.Server.ConnectedClients)
var clients = GameMain.Server.ConnectedClients.ToArray();
foreach (Client client in clients)
{
if (!client.HasPermission(ClientPermissions.ServerLog)) continue;
//use sendername as the message type

View File

@@ -673,13 +673,17 @@ namespace Barotrauma
}
}
private static readonly List<Entity> triggerersToRemove = new List<Entity>();
public static void RemoveInActiveTriggerers(PhysicsBody physicsBody, HashSet<Entity> triggerers)
{
if (physicsBody == null) { return; }
triggerersToRemove.Clear();
foreach (var triggerer in triggerers)
// Use local list instead of static field to avoid concurrent access issues during parallel updates
var triggerersToRemove = new List<Entity>();
// Create snapshot to avoid concurrent modification during enumeration
var triggererSnapshot = triggerers.ToArray();
foreach (var triggerer in triggererSnapshot)
{
if (triggerer.Removed)
{

View File

@@ -834,6 +834,12 @@ namespace Barotrauma
if (!IsValidValue(simPosition, "position", -1e10f, 1e10f)) { return false; }
if (!IsValidValue(rotation, "rotation")) { return false; }
if (PhysicsBodyQueue.IsInParallelContext)
{
PhysicsBodyQueue.Enqueue(() => SetTransform(simPosition, rotation, setPrevTransform));
return true;
}
FarseerBody.SetTransform(simPosition, rotation);
if (setPrevTransform) { SetPrevTransform(simPosition, rotation); }
return true;
@@ -848,6 +854,12 @@ namespace Barotrauma
if (!IsValidValue(simPosition, "position", -1e10f, 1e10f)) { return false; }
if (!IsValidValue(rotation, "rotation")) { return false; }
if (PhysicsBodyQueue.IsInParallelContext)
{
PhysicsBodyQueue.Enqueue(() => SetTransformIgnoreContacts(simPosition, rotation, setPrevTransform));
return true;
}
FarseerBody.SetTransformIgnoreContacts(ref simPosition, rotation);
if (setPrevTransform) { SetPrevTransform(simPosition, rotation); }
return true;