Skip to content

Commit c459605

Browse files
authored
Fix InitializeConstructors race condition (#371)
1 parent b138092 commit c459605

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

src/SIL.LCModel/Infrastructure/Impl/CmObjectSurrogate.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace SIL.LCModel.Infrastructure.Impl
3333
internal sealed class CmObjectSurrogate : ICmObjectSurrogate //, IEquatable<CmObjectSurrogate>
3434
{
3535
private static Dictionary<string, ConstructorInfo> s_classToConstructorInfo;
36+
private static readonly object s_constructorLock = new object();
3637
/// <summary>
3738
/// It's common that hundreds of thousands of surrogates only use a few hundred class names. This is a local interning
3839
/// of those names.
@@ -225,16 +226,19 @@ internal static CmObjectSurrogate CreateSnapshot(ICmObject obj)
225226

226227
internal static void InitializeConstructors(List<Type> cmObjectTypes)
227228
{
228-
if (s_classToConstructorInfo != null) return;
229-
230-
s_classToConstructorInfo = new Dictionary<string, ConstructorInfo>();
231-
// Get default constructor.
232-
// Only do this once, since they are stored in a static data member.
233-
foreach (var lcmType in cmObjectTypes)
229+
lock (s_constructorLock)
234230
{
235-
if (lcmType.IsAbstract) continue;
231+
if (s_classToConstructorInfo != null) return;
232+
233+
s_classToConstructorInfo = new Dictionary<string, ConstructorInfo>();
234+
// Get default constructor.
235+
// Only do this once, since they are stored in a static data member.
236+
foreach (var lcmType in cmObjectTypes)
237+
{
238+
if (lcmType.IsAbstract) continue;
236239

237-
s_classToConstructorInfo.Add(lcmType.Name, lcmType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
240+
s_classToConstructorInfo.Add(lcmType.Name, lcmType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
241+
}
238242
}
239243
}
240244

0 commit comments

Comments
 (0)