Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing debug assertion in JsonTypeInfo initialization #67334

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;

namespace System.Text.Json.Serialization.Metadata
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down