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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user