Skip to content

Commit

Permalink
Atomically swap config data (dotnet/extensions#1202)
Browse files Browse the repository at this point in the history
Swap the configuration providers' data atomically, rather than directly changing the property, so that any enumeration of the dictionary running during the reload operation does not throw an InvalidOperationException due to the collection being modified.
Relates to dotnet/extensions#1189.


Commit migrated from dotnet/extensions@192abfd
  • Loading branch information
martincostello authored and Tratcher committed Mar 4, 2019
1 parent fbc1427 commit c34d274
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void Load()

internal void Load(IDictionary envVariables)
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

var filteredEnvVariables = envVariables
.Cast<DictionaryEntry>()
Expand All @@ -58,8 +58,10 @@ internal void Load(IDictionary envVariables)
foreach (var envVariable in filteredEnvVariables)
{
var key = ((string)envVariable.Key).Substring(_prefix.Length);
Data[key] = (string)envVariable.Value;
data[key] = (string)envVariable.Value;
}

Data = data;
}

private static string NormalizeKey(string key)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration.Test;
using Xunit;

Expand Down Expand Up @@ -148,5 +152,48 @@ public void ReplaceDoubleUnderscoreInEnvironmentVariables()
Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:_db1_ProviderName"));
}

[Fact]
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
var dic = new Dictionary<string, string>
{
{"Number", "-2"},
{"Text", "Foo"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
configurationBuilder.AddEnvironmentVariables();
var config = configurationBuilder.Build();

MyOptions options = null;

using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
void ReloadLoop()
{
while (!cts.IsCancellationRequested)
{
config.Reload();
}
}

_ = Task.Run(ReloadLoop);

while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}
}

Assert.Equal(-2, options.Number);
Assert.Equal("Foo", options.Text);
}

private sealed class MyOptions
{
public int Number { get; set; }
public string Text { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Configuration.Ini;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration.Xml;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Configuration.Xml;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Xunit;
Expand Down Expand Up @@ -944,6 +944,48 @@ await WaitForChange(
Assert.True(token.HasChanged);
}

[Fact]
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
WriteTestFiles();

var configurationBuilder = CreateBuilder();
configurationBuilder.Add(new TestIniSourceProvider(_iniFile));
configurationBuilder.Add(new TestJsonSourceProvider(_jsonFile));
configurationBuilder.Add(new TestXmlSourceProvider(_xmlFile));
configurationBuilder.AddEnvironmentVariables();
configurationBuilder.AddCommandLine(new[] { "--CmdKey1=CmdValue1" });
configurationBuilder.AddInMemoryCollection(_memConfigContent);

var config = configurationBuilder.Build();

using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
void ReloadLoop()
{
while (!cts.IsCancellationRequested)
{
config.Reload();
}
}

_ = Task.Run(ReloadLoop);

MyOptions options = null;

while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}

Assert.Equal("CmdValue1", options.CmdKey1);
Assert.Equal("IniValue1", options.IniKey1);
Assert.Equal("JsonValue1", options.JsonKey1);
Assert.Equal("MemValue1", options.MemKey1);
Assert.Equal("XmlValue1", options.XmlKey1);
}
}

public void Dispose()
{
_fileProvider.Dispose();
Expand All @@ -966,5 +1008,18 @@ private async Task WaitForChange(
await Task.Delay(_msDelay);
}
}

private sealed class MyOptions
{
public string CmdKey1 { get; set; }

public string IniKey1 { get; set; }

public string JsonKey1 { get; set; }

public string MemKey1 { get; set; }

public string XmlKey1 { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.Ini" />
<Reference Include="Microsoft.Extensions.Configuration.Json" />
<Reference Include="Microsoft.Extensions.Configuration.Xml" />
Expand Down

0 comments on commit c34d274

Please sign in to comment.