Use local mutexes (#10651)

* Update file config mutex code in KeyboardManager

* Update runner instance mutex names and logic

* Update Launcher mutex

* Update a mutex in the Runner

* Restored a mutex used in the installer

* Update src/modules/launcher/PowerLauncher/App.xaml.cs

Co-authored-by: Enrico Giordani <enricogior@users.noreply.github.com>
This commit is contained in:
Ivan Stošić
2021-04-08 19:42:46 +02:00
committed by GitHub
parent aa5ff65b54
commit c08be14919
10 changed files with 33 additions and 17 deletions

View File

@@ -87,6 +87,7 @@
<ClInclude Include="KeyboardHook.h" /> <ClInclude Include="KeyboardHook.h" />
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />
<ClInclude Include="resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="shared_constants.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Generated Files\AssemblyInfo.cpp" /> <ClCompile Include="Generated Files\AssemblyInfo.cpp" />

View File

@@ -27,6 +27,9 @@
<ClInclude Include="resource.h"> <ClInclude Include="resource.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="shared_constants.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="interop.cpp"> <ClCompile Include="interop.cpp">

View File

@@ -160,5 +160,9 @@ public
static String ^ ShowShortcutGuideSharedEvent() { static String ^ ShowShortcutGuideSharedEvent() {
return gcnew String(CommonSharedConstants::SHOW_SHORTCUT_GUIDE_SHARED_EVENT); return gcnew String(CommonSharedConstants::SHOW_SHORTCUT_GUIDE_SHARED_EVENT);
} }
static String ^ KeyboardManagerConfigFileMutexName() {
return gcnew String(CommonSharedConstants::KEYBOARD_MANAGER_CONFIG_FILE_MUTEX_NAME);
}
}; };
} }

View File

@@ -27,4 +27,7 @@ namespace CommonSharedConstants
// Max DWORD for key code to disable keys. // Max DWORD for key code to disable keys.
const int VK_DISABLED = 0x100; const int VK_DISABLED = 0x100;
// Name of the mutex which controls access to the configuration file for Keyboard Manager
const wchar_t KEYBOARD_MANAGER_CONFIG_FILE_MUTEX_NAME[] = L"Local\\PowerToys_KeyboardManager_ConfigFileMutex";
} }

View File

@@ -10,17 +10,13 @@
namespace namespace
{ {
constexpr inline wchar_t POWERTOYS_MSI_MUTEX_NAME[] = L"Local\\PowerToyRunMutex"; constexpr inline wchar_t POWERTOYS_MSI_MUTEX_NAME[] = L"Local\\PowerToys_Runner_MSI_InstanceMutex";
constexpr inline wchar_t POWERTOYS_MSIX_MUTEX_NAME[] = L"Local\\PowerToyMSIXRunMutex"; constexpr inline wchar_t POWERTOYS_MSIX_MUTEX_NAME[] = L"Local\\PowerToys_Runner_MSIX_InstanceMutex";
constexpr inline wchar_t POWERTOYS_BOOTSTRAPPER_MUTEX_NAME[] = L"PowerToysBootstrapperMutex"; constexpr inline wchar_t POWERTOYS_BOOTSTRAPPER_MUTEX_NAME[] = L"Local\\PowerToys_Bootstrapper_InstanceMutex";
} }
inline wil::unique_mutex_nothrow createAppMutex(std::wstring mutexName) inline wil::unique_mutex_nothrow createAppMutex(const std::wstring& mutexName)
{ {
wchar_t username[UNLEN + 1];
DWORD username_length = UNLEN + 1;
GetUserNameW(username, &username_length);
mutexName += username;
wil::unique_mutex_nothrow result{ CreateMutexW(nullptr, TRUE, mutexName.c_str()) }; wil::unique_mutex_nothrow result{ CreateMutexW(nullptr, TRUE, mutexName.c_str()) };
return GetLastError() == ERROR_ALREADY_EXISTS ? wil::unique_mutex_nothrow{} : std::move(result); return GetLastError() == ERROR_ALREADY_EXISTS ? wil::unique_mutex_nothrow{} : std::move(result);

View File

@@ -3,6 +3,7 @@
#include <winrt/base.h> #include <winrt/base.h>
#include <common/utils/resources.h> #include <common/utils/resources.h>
#include "keyboardmanager/dll/Generated Files/resource.h" #include "keyboardmanager/dll/Generated Files/resource.h"
#include "common/interop/shared_constants.h"
namespace KeyboardManagerConstants namespace KeyboardManagerConstants
{ {
@@ -40,7 +41,7 @@ namespace KeyboardManagerConstants
inline const std::wstring DefaultConfiguration = L"default"; inline const std::wstring DefaultConfiguration = L"default";
// Name of the named mutex used for configuration file. // Name of the named mutex used for configuration file.
inline const std::wstring ConfigFileMutexName = L"PowerToys.KeyboardManager.ConfigMutex"; inline const std::wstring ConfigFileMutexName = CommonSharedConstants::KEYBOARD_MANAGER_CONFIG_FILE_MUTEX_NAME;
// Name of the dummy update file. // Name of the dummy update file.
inline const std::wstring DummyUpdateFileName = L"settings-updated.json"; inline const std::wstring DummyUpdateFileName = L"settings-updated.json";

View File

@@ -29,7 +29,7 @@ namespace PowerLauncher
{ {
public static PublicAPIInstance API { get; private set; } public static PublicAPIInstance API { get; private set; }
private const string Unique = "PowerLauncher_Unique_Application_Mutex"; private const string Unique = "PowerToys_PowerToysRun_InstanceMutex";
private static bool _disposed; private static bool _disposed;
private PowerToysRunSettings _settings; private PowerToysRunSettings _settings;
private MainViewModel _mainVM; private MainViewModel _mainVM;

View File

@@ -44,6 +44,11 @@ namespace PowerLauncher.Helper
/// </summary> /// </summary>
private const string ChannelNameSuffix = "SingeInstanceIPCChannel"; private const string ChannelNameSuffix = "SingeInstanceIPCChannel";
/// <summary>
/// Prefix to the names of mutexes which ensures they are unique in a Windows session.
/// </summary>
private const string LocalMutexPrefix = @"Local\";
/// <summary> /// <summary>
/// Gets or sets application mutex. /// Gets or sets application mutex.
/// </summary> /// </summary>
@@ -62,7 +67,8 @@ namespace PowerLauncher.Helper
string channelName = string.Concat(applicationIdentifier, Delimiter, ChannelNameSuffix); string channelName = string.Concat(applicationIdentifier, Delimiter, ChannelNameSuffix);
// Create mutex based on unique application Id to check if this is the first instance of the application. // Create mutex based on unique application Id to check if this is the first instance of the application.
SingleInstanceMutex = new Mutex(true, applicationIdentifier, out bool firstInstance); string mutexName = string.Concat(LocalMutexPrefix, uniqueName);
SingleInstanceMutex = new Mutex(true, mutexName, out bool firstInstance);
if (firstInstance) if (firstInstance)
{ {
_ = CreateRemoteService(channelName); _ = CreateRemoteService(channelName);

View File

@@ -8,7 +8,7 @@
namespace namespace
{ {
const wchar_t PERSISTENT_STATE_FILENAME[] = L"\\update_state.json"; const wchar_t PERSISTENT_STATE_FILENAME[] = L"\\update_state.json";
const wchar_t UPDATE_STATE_MUTEX[] = L"PTUpdateStateMutex"; const wchar_t UPDATE_STATE_MUTEX[] = L"Local\\PowerToys_Runner_UpdateStateMutex";
} }
UpdateState deserialize(const json::JsonObject& json) UpdateState deserialize(const json::JsonObject& json)

View File

@@ -29,8 +29,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private const string EditShortcutActionName = "EditShortcut"; private const string EditShortcutActionName = "EditShortcut";
private const string EditShortcutActionValue = "Open Edit Shortcut Window"; private const string EditShortcutActionValue = "Open Edit Shortcut Window";
private const string JsonFileType = ".json"; private const string JsonFileType = ".json";
private const string ProfileFileMutexName = "PowerToys.KeyboardManager.ConfigMutex";
private const int ProfileFileMutexWaitTimeoutMilliseconds = 1000; private static string ConfigFileMutexName => interop.Constants.KeyboardManagerConfigFileMutexName();
private const int ConfigFileMutexWaitTimeoutMilliseconds = 1000;
public KeyboardManagerSettings Settings { get; set; } public KeyboardManagerSettings Settings { get; set; }
@@ -203,9 +205,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
try try
{ {
using (var profileFileMutex = Mutex.OpenExisting(ProfileFileMutexName)) using (var profileFileMutex = Mutex.OpenExisting(ConfigFileMutexName))
{ {
if (profileFileMutex.WaitOne(ProfileFileMutexWaitTimeoutMilliseconds)) if (profileFileMutex.WaitOne(ConfigFileMutexWaitTimeoutMilliseconds))
{ {
// update the UI element here. // update the UI element here.
try try