From 06a96c276ddc3ce3f9630643f4c7e8ccc9db0ebd Mon Sep 17 00:00:00 2001 From: Nikita Petko Date: Tue, 18 Jun 2024 19:13:07 +0100 Subject: [PATCH] #23: Prevent duplicate providers (#24) ~ Add locking to maybe ensure that providers are not registered more than once. More testing will be needed to determine why there are more than one to begin with. --- .../Implementation/VaultProvider.cs | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/configuration/configuration/Implementation/VaultProvider.cs b/src/configuration/configuration/Implementation/VaultProvider.cs index 97b3497..50534d9 100644 --- a/src/configuration/configuration/Implementation/VaultProvider.cs +++ b/src/configuration/configuration/Implementation/VaultProvider.cs @@ -164,7 +164,17 @@ protected VaultProvider(ILogger logger = null) _logger?.Debug("VaultProvider: Setup for '{0}/{1}' to refresh every '{2}' interval!", Mount, Path, RefreshInterval); - _providers.Add(this); + lock (_providers) + { + if (_providers.Contains(this)) + { + _logger?.Debug("VaultProvider: Skipping setup for '{0}/{1}' because it is already setup!", Mount, Path); + + return; + } + + _providers.Add(this); + } DoRefresh(); } @@ -173,21 +183,24 @@ private static void RefreshThread() { while (true) { - var providers = _providers.ToArray(); - - foreach (var provider in providers) + lock (_providers) { - try - { - provider.DoRefresh(); - } - catch (Exception ex) + var providers = _providers.ToArray(); + + foreach (var provider in providers) { - _staticLogger?.Error(ex); + try + { + provider.DoRefresh(); + } + catch (Exception ex) + { + _staticLogger?.Error(ex); + } } - } - Thread.Sleep(RefreshInterval); // SetClient makes DoRefresh call. + Thread.Sleep(RefreshInterval); // SetClient makes DoRefresh call. + } } }