diff --git a/src/Microsoft.Azure.Relay.Bridge/Configuration/Config.cs b/src/Microsoft.Azure.Relay.Bridge/Configuration/Config.cs index 6b97b1c..24b9780 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Configuration/Config.cs +++ b/src/Microsoft.Azure.Relay.Bridge/Configuration/Config.cs @@ -17,6 +17,7 @@ namespace Microsoft.Azure.Relay.Bridge.Configuration using System.Runtime.Serialization; using System.Text; using System.Text.RegularExpressions; + using System.Threading; using YamlDotNet.Core; /// @@ -1173,6 +1174,30 @@ public static Config LoadConfigFile(string fileName) if (File.Exists(fileName)) { + // test whether the file is locked for reading; if so we'll spin wait + // for a bit and then throw an exception + for (int i = 0; i < 10; i++) + { + try + { + using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None)) + { + if (!fs.CanRead) + { + throw BridgeEventSource.Log.ArgumentOutOfRange( + nameof(fileName), + $"Configuration file {fileName} is locked for reading", + null); + } + } + break; + } + catch (IOException) + { + Thread.Sleep(200); + } + } + using (var reader = new StreamReader(fileName)) { try diff --git a/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs b/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs index a03e020..4f9130e 100644 --- a/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs +++ b/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs @@ -903,9 +903,9 @@ public void ConfigLocalForwardTest() // Modify the config file string modifiedConfigContent = "LocalForward:\n" + - " - BindAddress : 127.0.97.1\n" + - " BindPort : 8009\n" + - " RelayName : a1\n"; + " - BindAddress : 127.0.97.1\n" + + " BindPort : 8009\n" + + " RelayName : a1\n"; File.WriteAllText(initialConfigPath, modifiedConfigContent); // this should have triggered the FS watcher and the config should have been reloaded Thread.Sleep(5000);