Skip to content

Commit f6319e4

Browse files
Prevent ObjectDisposedException if Key Vault config provider disposed twice (Azure#24769)
1 parent cbef31d commit f6319e4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

sdk/extensions/Azure.Extensions.AspNetCore.Configuration.Secrets/src/AzureKeyVaultConfigurationProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AzureKeyVaultConfigurationProvider : ConfigurationProvider, IDispos
2323
private Dictionary<string, KeyVaultSecret> _loadedSecrets;
2424
private Task _pollingTask;
2525
private readonly CancellationTokenSource _cancellationToken;
26+
private bool _disposed;
2627

2728
/// <summary>
2829
/// Creates a new instance of <see cref="AzureKeyVaultConfigurationProvider"/>.
@@ -148,8 +149,13 @@ protected virtual void Dispose(bool disposing)
148149
{
149150
if (disposing)
150151
{
151-
_cancellationToken.Cancel();
152-
_cancellationToken.Dispose();
152+
if (!_disposed)
153+
{
154+
_cancellationToken.Cancel();
155+
_cancellationToken.Dispose();
156+
}
157+
158+
_disposed = true;
153159
}
154160
}
155161

sdk/extensions/Azure.Extensions.AspNetCore.Configuration.Secrets/tests/AzureKeyVaultConfigurationTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,21 @@ public void ConstructorThrowsForNegativeRefreshPeriodValue()
628628
Assert.Throws<ArgumentOutOfRangeException>(() => new AzureKeyVaultConfigurationProvider(Mock.Of<SecretClient>(), new AzureKeyVaultConfigurationOptions() { ReloadInterval = TimeSpan.FromMilliseconds(-1) }));
629629
}
630630

631+
[Test]
632+
public void DisposeCanBeCalledMultipleTimes()
633+
{
634+
// Arrange
635+
var client = new Mock<SecretClient>();
636+
637+
using (var provider = new AzureKeyVaultConfigurationProvider(client.Object, new AzureKeyVaultConfigurationOptions() { Manager = new KeyVaultSecretManager() }))
638+
{
639+
provider.Dispose();
640+
641+
// Act & Assert
642+
Assert.DoesNotThrow(() => provider.Dispose());
643+
}
644+
}
645+
631646
private class EndsWithOneKeyVaultSecretManager : KeyVaultSecretManager
632647
{
633648
public override bool Load(SecretProperties secret)

0 commit comments

Comments
 (0)