Skip to content

Commit

Permalink
Lock EnsureConfigured to prevent initialization issues in JsonTypeInfo (
Browse files Browse the repository at this point in the history
#68605)

* Lock EnsureConfigured to prevent initialization issues in JsonTypeInfo

* do not use this for lock, ensure we call Configure from lock
  • Loading branch information
krwq authored Apr 28, 2022
1 parent aef3dc9 commit 7654bf2
Showing 1 changed file with 10 additions and 2 deletions.
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()
{
Debug.Assert(Monitor.IsEntered(_configureLock), "Configure called directly, use EnsureConfigured which locks this method");
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

0 comments on commit 7654bf2

Please sign in to comment.