From 96d1ddad4120d5a687c0ed4eb897a0ee7b988a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cant=C3=BA?= Date: Wed, 24 Sep 2025 18:02:11 -0500 Subject: [PATCH] Don't set access control if the mutex is not initially owned --- .../System/Diagnostics/NetFrameworkUtils.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/libraries/Common/src/System/Diagnostics/NetFrameworkUtils.cs b/src/libraries/Common/src/System/Diagnostics/NetFrameworkUtils.cs index 87f56dcdc242f4..e30106f020012a 100644 --- a/src/libraries/Common/src/System/Diagnostics/NetFrameworkUtils.cs +++ b/src/libraries/Common/src/System/Diagnostics/NetFrameworkUtils.cs @@ -22,14 +22,17 @@ internal static void EnterMutex(string name, ref Mutex mutex) internal static void EnterMutexWithoutGlobal(string mutexName, ref Mutex mutex) { - Mutex tmpMutex = new Mutex(false, mutexName, out _); - - // Specify a SID in case the mutex has not yet been created; this prevents it from using the SID from the current thread. - // This SID (AuthenticatedUserSid) is the same one used by .NET Framework. - MutexSecurity sec = new MutexSecurity(); - SecurityIdentifier authenticatedUserSid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); - sec.AddAccessRule(new MutexAccessRule(authenticatedUserSid, MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow)); - tmpMutex.SetAccessControl(sec); + Mutex tmpMutex = new Mutex(false, mutexName, out bool createdNew); + + if (createdNew) + { + // Specify a SID in case the mutex has not yet been created; this prevents it from using the SID from the current thread. + // This SID (AuthenticatedUserSid) is the same one used by .NET Framework. + MutexSecurity sec = new MutexSecurity(); + SecurityIdentifier authenticatedUserSid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); + sec.AddAccessRule(new MutexAccessRule(authenticatedUserSid, MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow)); + tmpMutex.SetAccessControl(sec); + } SafeWaitForMutex(tmpMutex, ref mutex); }