Skip to content

Commit 10a72f1

Browse files
committed
Ensure that no deadlock may occur when two files are swapped
1 parent b84d877 commit 10a72f1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,17 @@ private static async Task OnRenamedAsync(IRenamedFileSystemEvent rfse, Cancellat
551551
//NB! if file is renamed to cs~ or resx~ then that means there will be yet another write to same file, so lets skip this event here
552552
if (!rfse.FileSystemInfo.FullName.EndsWith("~"))
553553
{
554-
using (await FileLocks.LockAsync(rfse.FileSystemInfo.FullName, token))
555-
using (await FileLocks.LockAsync(rfse.PreviousFileSystemInfo.FullName, token))
554+
var filenames = new List<string>()
555+
{
556+
rfse.FileSystemInfo.FullName,
557+
rfse.PreviousFileSystemInfo.FullName
558+
};
559+
560+
//NB! in order to avoid deadlocks in case of file swaps, always take the locks in deterministic order
561+
filenames.Sort(StringComparer.InvariantCultureIgnoreCase);
562+
563+
using (await FileLocks.LockAsync(filenames[0], token))
564+
using (await FileLocks.LockAsync(filenames[1], token))
556565
{
557566
await FileUpdated(rfse.FileSystemInfo.FullName, context);
558567
await FileDeleted(rfse.PreviousFileSystemInfo.FullName, context);

0 commit comments

Comments
 (0)