Skip to content

Commit

Permalink
Ensure JsonTypeInfo.Configure() is called once even when exceptions a…
Browse files Browse the repository at this point in the history
…re thrown (#71630)

* Ensure JsonTypeInfo.Configure() is called once even when exceptions are thrown

* Add cached exception check in critical section
  • Loading branch information
eiriktsarpalis committed Jul 5, 2022
1 parent 3f606e5 commit a583698
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading;

namespace System.Text.Json.Serialization.Metadata
Expand Down Expand Up @@ -404,22 +405,38 @@ internal void VerifyMutable()

private protected volatile bool _isConfigured;
private readonly object _configureLock = new object();
private ExceptionDispatchInfo? _cachedConfigureError;

internal bool IsConfigured => _isConfigured;

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

lock (_configureLock)
void ConfigureLocked()
{
if (_isConfigured)
return;
_cachedConfigureError?.Throw();

lock (_configureLock)
{
if (_isConfigured)
return;

_cachedConfigureError?.Throw();

Configure();
try
{
Configure();

_isConfigured = true;
_isConfigured = true;
}
catch (Exception e)
{
_cachedConfigureError = ExceptionDispatchInfo.Capture(e);
throw;
}
}
}
}

Expand Down

0 comments on commit a583698

Please sign in to comment.