Skip to content

Commit

Permalink
Prevent ObjectDisposedException if Key Vault config provider disposed…
Browse files Browse the repository at this point in the history
… twice (#24769)
  • Loading branch information
martincostello authored Oct 25, 2021
1 parent cbef31d commit f6319e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AzureKeyVaultConfigurationProvider : ConfigurationProvider, IDispos
private Dictionary<string, KeyVaultSecret> _loadedSecrets;
private Task _pollingTask;
private readonly CancellationTokenSource _cancellationToken;
private bool _disposed;

/// <summary>
/// Creates a new instance of <see cref="AzureKeyVaultConfigurationProvider"/>.
Expand Down Expand Up @@ -148,8 +149,13 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_cancellationToken.Cancel();
_cancellationToken.Dispose();
if (!_disposed)
{
_cancellationToken.Cancel();
_cancellationToken.Dispose();
}

_disposed = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,21 @@ public void ConstructorThrowsForNegativeRefreshPeriodValue()
Assert.Throws<ArgumentOutOfRangeException>(() => new AzureKeyVaultConfigurationProvider(Mock.Of<SecretClient>(), new AzureKeyVaultConfigurationOptions() { ReloadInterval = TimeSpan.FromMilliseconds(-1) }));
}

[Test]
public void DisposeCanBeCalledMultipleTimes()
{
// Arrange
var client = new Mock<SecretClient>();

using (var provider = new AzureKeyVaultConfigurationProvider(client.Object, new AzureKeyVaultConfigurationOptions() { Manager = new KeyVaultSecretManager() }))
{
provider.Dispose();

// Act & Assert
Assert.DoesNotThrow(() => provider.Dispose());
}
}

private class EndsWithOneKeyVaultSecretManager : KeyVaultSecretManager
{
public override bool Load(SecretProperties secret)
Expand Down

0 comments on commit f6319e4

Please sign in to comment.