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); }