Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ObjectDisposedException if Key Vault config provider disposed twice #24769

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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