Skip to content

Commit

Permalink
Wait for locked file
Browse files Browse the repository at this point in the history
  • Loading branch information
clemensv committed Sep 4, 2024
1 parent 8130439 commit 993fa63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Microsoft.Azure.Relay.Bridge/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 993fa63

Please sign in to comment.