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

Lock EnsureConfigured to prevent initialization issues in JsonTypeInfo #68605

Merged
merged 2 commits into from
Apr 28, 2022
Merged
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 @@ -195,19 +195,27 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions
}

private bool _isConfigured;
private object _configureLock = new object();

internal void EnsureConfigured()
{
if (_isConfigured)
return;

Configure();
lock (_configureLock)
{
if (_isConfigured)
return;

Configure();

_isConfigured = true;
_isConfigured = true;
}
}

internal virtual void Configure()
krwq marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Assert(Monitor.IsEntered(_configureLock), "Configure called directly, use EnsureConfigured which locks this method");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is virtual, might make sense to add the same to any overrides?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they will call base.Configure anyway

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. That might change in future iterations, but I guess nothing prevents users from overriding the Configure method elsewhere either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the method be changed to protected virtual instead?

JsonConverter converter = PropertyInfoForTypeInfo.ConverterBase;
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == PropertyInfoForTypeInfo.ConverterBase.ConverterStrategy,
$"ConverterStrategy from PropertyInfoForTypeInfo.ConverterStrategy ({PropertyInfoForTypeInfo.ConverterStrategy}) does not match converter's ({PropertyInfoForTypeInfo.ConverterBase.ConverterStrategy})");
Expand Down