Skip to content

Commit ce2a70d

Browse files
Don't set access control if the mutex is not initially owned (#120115)
Co-authored-by: David Cantú <[email protected]>
1 parent 4c588fe commit ce2a70d

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/libraries/Common/src/System/Diagnostics/NetFrameworkUtils.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ internal static void EnterMutex(string name, ref Mutex mutex)
2222

2323
internal static void EnterMutexWithoutGlobal(string mutexName, ref Mutex mutex)
2424
{
25-
Mutex tmpMutex = new Mutex(false, mutexName, out _);
26-
27-
// Specify a SID in case the mutex has not yet been created; this prevents it from using the SID from the current thread.
28-
// This SID (AuthenticatedUserSid) is the same one used by .NET Framework.
29-
MutexSecurity sec = new MutexSecurity();
30-
SecurityIdentifier authenticatedUserSid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
31-
sec.AddAccessRule(new MutexAccessRule(authenticatedUserSid, MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow));
32-
tmpMutex.SetAccessControl(sec);
25+
Mutex tmpMutex = new Mutex(false, mutexName, out bool createdNew);
26+
27+
if (createdNew)
28+
{
29+
// Specify a SID in case the mutex has not yet been created; this prevents it from using the SID from the current thread.
30+
// This SID (AuthenticatedUserSid) is the same one used by .NET Framework.
31+
MutexSecurity sec = new MutexSecurity();
32+
SecurityIdentifier authenticatedUserSid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
33+
sec.AddAccessRule(new MutexAccessRule(authenticatedUserSid, MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow));
34+
tmpMutex.SetAccessControl(sec);
35+
}
3336

3437
SafeWaitForMutex(tmpMutex, ref mutex);
3538
}

0 commit comments

Comments
 (0)