Avoid modification of the list while iterating over it

This commit is contained in:
Thorsten Sommer 2025-08-19 13:39:35 +02:00
parent 80c1d72ca2
commit 589d62c9ce
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -44,8 +44,8 @@ public sealed record PluginConfigurationObject
IList<IAvailablePlugin> availablePlugins,
IList<PluginConfigurationObject> configObjectList) where TClass : IConfigurationObject
{
var wasConfigurationChanged = false;
var configuredObjects = configObjectSelection.Compile()(SETTINGS_MANAGER.ConfigurationData);
var leftOverObjects = new List<TClass>();
foreach (var configuredObject in configuredObjects)
{
if(!configuredObject.IsEnterpriseConfiguration)
@ -59,8 +59,7 @@ public sealed record PluginConfigurationObject
if(templateSourcePlugin is null)
{
LOG.LogWarning($"The configured object '{configuredObject.Name}' (id={configuredObject.Id}) is based on a plugin that is not available anymore. Removing the chat template from the settings.");
configuredObjects.Remove(configuredObject);
wasConfigurationChanged = true;
leftOverObjects.Add(configuredObject);
}
if(!configObjectList.Any(configObject =>
@ -69,11 +68,15 @@ public sealed record PluginConfigurationObject
configObject.Id.ToString() == configuredObject.Id))
{
LOG.LogWarning($"The configured object '{configuredObject.Name}' (id={configuredObject.Id}) is not present in the configuration plugin anymore. Removing the chat template from the settings.");
configuredObjects.Remove(configuredObject);
wasConfigurationChanged = true;
leftOverObjects.Add(configuredObject);
}
}
// Remove collected items after enumeration to avoid modifying the collection during iteration:
var wasConfigurationChanged = leftOverObjects.Count > 0;
foreach (var item in leftOverObjects.Distinct())
configuredObjects.Remove(item);
return wasConfigurationChanged;
}
}