未验证 提交 203a9016 编写于 作者: E Eirik Tsarpalis 提交者: GitHub

Fix failing debug assertion in JsonTypeInfo initialization (#67334)

上级 5a0564b0
......@@ -6,6 +6,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace System.Text.Json.Serialization.Metadata
{
......@@ -571,9 +572,16 @@ internal void UpdateSortedParameterCache(ref ReadStackFrame frame)
internal void InitializePropCache()
{
Debug.Assert(PropertyCache == null);
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object);
// Delayed JsonTypeInfo initialization can be invoked by
// multiple threads, add a temporary check to avoid contention.
// TODO refactor so that metadata initialization is single threaded.
if (Volatile.Read(ref PropertyCache) is not null)
{
return;
}
JsonSerializerContext? context = Options.JsonSerializerContext;
Debug.Assert(context != null);
......@@ -620,16 +628,23 @@ internal void InitializePropCache()
CacheMember(jsonPropertyInfo, propertyCache, ref ignoredMembers);
}
// Avoid threading issues by populating a local cache and assigning it to the global cache after completion.
PropertyCache = propertyCache;
// Populate a local cache and assign it to the global cache after completion.
Volatile.Write(ref PropertyCache, propertyCache);
}
internal void InitializeParameterCache()
{
Debug.Assert(ParameterCache == null);
Debug.Assert(PropertyCache != null);
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object);
// Delayed JsonTypeInfo initialization can be invoked by
// multiple threads, add a temporary check to avoid contention.
// TODO refactor so that metadata initialization is single threaded.
if (Volatile.Read(ref ParameterCache) is not null)
{
return;
}
JsonSerializerContext? context = Options.JsonSerializerContext;
Debug.Assert(context != null);
......
......@@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Reflection;
using System.Threading;
namespace System.Text.Json.Serialization.Metadata
{
......@@ -487,8 +488,8 @@ private void InitializeConstructorParameters(JsonParameterInfoValues[] jsonParam
}
}
ParameterCache = parameterCache;
ParameterCount = jsonParameters.Length;
Volatile.Write(ref ParameterCache, parameterCache);
}
private static JsonParameterInfoValues[] GetParameterInfoArray(ParameterInfo[] parameters)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册