Add lock for IniConfig.SectionMap

This commit is contained in:
Christian Schulz 2025-05-30 21:34:33 +02:00
commit 4e7da54a22

View file

@ -41,6 +41,11 @@ namespace Greenshot.Base.IniFile
/// </summary> /// </summary>
private static readonly object IniLock = new object(); private static readonly object IniLock = new object();
/// <summary>
/// A lock object for the section map
/// </summary>
private static readonly object SectionMapLock = new object();
/// <summary> /// <summary>
/// As the ini implementation is kept someone generic, for reusing, this holds the name of the application /// As the ini implementation is kept someone generic, for reusing, this holds the name of the application
/// </summary> /// </summary>
@ -290,23 +295,26 @@ namespace Greenshot.Base.IniFile
// Load the fixed settings // Load the fixed settings
_fixedProperties = Read(CreateIniLocation(_configName + FixedPostfix + IniExtension, true)); _fixedProperties = Read(CreateIniLocation(_configName + FixedPostfix + IniExtension, true));
foreach (IniSection section in SectionMap.Values) lock (SectionMapLock)
{ {
try foreach (IniSection section in SectionMap.Values)
{ {
section.Fill(PropertiesForSection(section)); try
FixProperties(section);
}
catch (Exception ex)
{
string sectionName = "unknown";
if (section?.IniSectionAttribute?.Name != null)
{ {
sectionName = section.IniSectionAttribute.Name; section.Fill(PropertiesForSection(section));
FixProperties(section);
} }
catch (Exception ex)
{
string sectionName = "unknown";
if (section?.IniSectionAttribute?.Name != null)
{
sectionName = section.IniSectionAttribute.Name;
}
Log.WarnFormat("Problem reading the ini section {0}", sectionName); Log.WarnFormat("Problem reading the ini section {0}", sectionName);
Log.Warn("Exception", ex); Log.Warn("Exception", ex);
}
} }
} }
} }
@ -389,7 +397,12 @@ namespace Greenshot.Base.IniFile
{ {
get get
{ {
foreach (string sectionName in SectionMap.Keys) List<string> sectionNames;
lock (SectionMapLock)
{
sectionNames = [.. SectionMap.Keys];
}
foreach (string sectionName in sectionNames)
{ {
yield return sectionName; yield return sectionName;
} }
@ -403,8 +416,11 @@ namespace Greenshot.Base.IniFile
/// <returns></returns> /// <returns></returns>
public static IniSection GetIniSection(string sectionName) public static IniSection GetIniSection(string sectionName)
{ {
SectionMap.TryGetValue(sectionName, out var returnValue); lock (SectionMapLock)
return returnValue; {
SectionMap.TryGetValue(sectionName, out var returnValue);
return returnValue;
}
} }
/// <summary> /// <summary>
@ -429,20 +445,24 @@ namespace Greenshot.Base.IniFile
Type iniSectionType = typeof(T); Type iniSectionType = typeof(T);
string sectionName = IniSection.GetIniSectionAttribute(iniSectionType).Name; string sectionName = IniSection.GetIniSectionAttribute(iniSectionType).Name;
if (SectionMap.ContainsKey(sectionName))
lock (SectionMapLock)
{ {
if (SectionMap.ContainsKey(sectionName))
{
//LOG.Debug("Returning pre-mapped section " + sectionName); //LOG.Debug("Returning pre-mapped section " + sectionName);
section = (T) SectionMap[sectionName]; section = (T)SectionMap[sectionName];
} }
else else
{ {
// Create instance of this type // Create instance of this type
section = (T) Activator.CreateInstance(iniSectionType); section = (T)Activator.CreateInstance(iniSectionType);
// Store for later save & retrieval // Store for later save & retrieval
SectionMap.Add(sectionName, section); SectionMap.Add(sectionName, section);
section.Fill(PropertiesForSection(section)); section.Fill(PropertiesForSection(section));
FixProperties(section); FixProperties(section);
}
} }
if (allowSave && section.IsDirty) if (allowSave && section.IsDirty)