Skip to content

Commit

Permalink
Protect AppSettings saving using global mutex (#10193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstv committed Oct 5, 2022
1 parent 12cb4e2 commit 22263a5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion GitCommands/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using GitCommands.Settings;
using GitExtUtils;
Expand Down Expand Up @@ -40,6 +41,8 @@ public static class AppSettings

private static readonly SettingsPath DetailedSettingsPath = new AppSettingsPath("Detailed");

private static Mutex _globalMutex;

public static readonly int BranchDropDownMinWidth = 300;
public static readonly int BranchDropDownMaxWidth = 600;

Expand Down Expand Up @@ -1603,7 +1606,18 @@ public static void SaveSettings()
{
SettingsContainer.LockedAction(() =>
{
SettingsContainer.Save();
// prepend "Global\" in order to be safe in preparation for non-Windows OS, too
_globalMutex ??= new Mutex(initiallyOwned: false, name: @$"Global\Mutex{SettingsFilePath.ToPosixPath()}");
try
{
_globalMutex.WaitOne();
SettingsContainer.Save();
}
finally
{
_globalMutex.ReleaseMutex();
}
});

Saved?.Invoke();
Expand Down

0 comments on commit 22263a5

Please sign in to comment.