Skip to content

Commit

Permalink
Improve handling of Directory creation on linux for FileDistributedLocks
Browse files Browse the repository at this point in the history
Fix #195
  • Loading branch information
madelson committed Mar 17, 2024
1 parent 229bd7d commit 607bbe6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/DistributedLock.FileSystem/FileDistributedLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ private void EnsureDirectoryExists()
System.IO.Directory.CreateDirectory(this.Directory);
return;
}
// This can indicate either a transient failure during concurrent creation/deletion or a permissions issue.
// If we encounter it, assume it is transient unless it persists.
//catch (UnauthorizedAccessException) when (++retryCount <= MaxUnauthorizedAccessExceptionRetries) { }
catch (Exception ex)
{
// This can indicate either a transient failure during concurrent creation/deletion or a permissions issue.
// If we encounter it, assume it is transient unless it persists.
// For a long time, I just checked for UnauthorizedAccessException here. However, recent tests on Linux have
// shown that in race conditions we can see IOException as well, presumably because there is some period during
// directory creation where it presents as a file.
if (ex is UnauthorizedAccessException or IOException
&& retryCount < MaxUnauthorizedAccessExceptionRetries)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public void TestThrowsIfProvidedFileNameIsAlreadyADirectory()
Assert.That(exception.Message, Does.Contain("because it is already the name of a directory"));
}

[Test]
public void TestThrowsIfProvidedDirectoryIsAlreadyFile()
{
var tempFile = Path.GetTempFileName();

var @lock = new FileDistributedLock(lockFileDirectory: new(tempFile), "some name");
var exception = Assert.Throws<InvalidOperationException>(() => @lock.Acquire().Dispose());
Assert.That(exception.InnerException!.Message, Does.Match("file .* already exists"));
}

[Test]
public void TestEmptyNameIsAllowed() => AssertCanUseName(string.Empty);

Expand Down

0 comments on commit 607bbe6

Please sign in to comment.