Skip to content

Commit

Permalink
Fix potential ArgumentException with ConditionalWeakTable
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetrou committed Jan 9, 2025
1 parent f5d289e commit f88ffcf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ internal sealed class DefaultRequestResponseSerializer : SystemTextJsonSerialize
{
private readonly IElasticsearchClientSettings _settings;

#if !NET8_0_OR_GREATER
private readonly object _lock = new();
#endif

public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings) :
base(new DefaultRequestResponseSerializerOptionsProvider(settings))
{
Expand Down Expand Up @@ -62,15 +66,23 @@ private void LinkSettings(IElasticsearchClientSettings settings)
var options = GetJsonSerializerOptions(SerializationFormatting.None);
var indentedOptions = GetJsonSerializerOptions(SerializationFormatting.Indented);

if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _))
{
ElasticsearchClient.SettingsTable.Add(options, settings);
}

if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _))
#if NET8_0_OR_GREATER
ElasticsearchClient.SettingsTable.TryAdd(options, settings);
ElasticsearchClient.SettingsTable.TryAdd(indentedOptions, settings);
#else
lock (_lock)
{
ElasticsearchClient.SettingsTable.Add(indentedOptions, settings);
if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _))
{
ElasticsearchClient.SettingsTable.Add(options, settings);
}

if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _))
{
ElasticsearchClient.SettingsTable.Add(indentedOptions, settings);
}
}
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ namespace Elastic.Clients.Elasticsearch.Serialization;
public class DefaultSourceSerializer :
SystemTextJsonSerializer
{
#if !NET8_0_OR_GREATER
private readonly object _lock = new();
#endif

/// <summary>
/// Constructs a new <see cref="DefaultSourceSerializer"/> instance that accepts an <see cref="Action{T}"/> that can
/// be provided to customize the default <see cref="JsonSerializerOptions"/>.
Expand All @@ -43,15 +47,23 @@ private void LinkSettings(IElasticsearchClientSettings settings)
var options = GetJsonSerializerOptions(SerializationFormatting.None);
var indentedOptions = GetJsonSerializerOptions(SerializationFormatting.Indented);

if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _))
#if NET8_0_OR_GREATER
ElasticsearchClient.SettingsTable.TryAdd(options, settings);
ElasticsearchClient.SettingsTable.TryAdd(indentedOptions, settings);
#else
lock (_lock)
{
ElasticsearchClient.SettingsTable.Add(options, settings);
}
if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _))
{
ElasticsearchClient.SettingsTable.Add(options, settings);
}

if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _))
{
ElasticsearchClient.SettingsTable.Add(indentedOptions, settings);
if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _))
{
ElasticsearchClient.SettingsTable.Add(indentedOptions, settings);
}
}
#endif
}
}

Expand Down

0 comments on commit f88ffcf

Please sign in to comment.