Add the CurrentUserOnly and CurrentSessionOnly options for named Mutex, Semaphore, and EventWaitHandle#112213
Merged
kouvel merged 8 commits intodotnet:mainfrom Mar 12, 2025
kouvel:NamedWhApis
Merged
Add the CurrentUserOnly and CurrentSessionOnly options for named Mutex, Semaphore, and EventWaitHandle#112213kouvel merged 8 commits intodotnet:mainfrom kouvel:NamedWhApis
CurrentUserOnly and CurrentSessionOnly options for named Mutex, Semaphore, and EventWaitHandle#112213kouvel merged 8 commits intodotnet:mainfrom
kouvel:NamedWhApis
Conversation
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mutex,Semaphore, andEventWaitHandle, added overloads for methods with anameargument to also include aNamedWaitHandleOptions optionsargument. TheCurrentUserOnlyoption indicates whether the object should be limited in access to the current user. TheCurrentSessionOnlyoption indicates whether the object object is intended to be used only within the current session (an alternative to the name prefixes, such asGlobal\). The default value forNamedWaitHandleOptionshasCurrentUserOnly=trueandCurrentSessionOnly=true.CurrentUserOnly=true. Sharing the CoreCLR Unix named mutex implementation with NativeAOT/Mono is left to a separate change. Most of what is described below whenCurrentUserOnly=trueon Unixes refers to the CoreCLR implementation.nameargument but without anoptionsargument in a separate changeWindows with
CurrentUserOnly=trueWhen creating a named mutex, a security descriptor is created and assigned to the object.
BUILTIN\Administratorsgroup theREAD_CONTROLaccess right to enable reading the security info for diagnostic purposesWaitHandleCannotBeOpenedExceptionis thrownUnixes with
CurrentUserOnly=true/tmp/.dotnet-uidN/whereNis the effective user ID/tmp/(or equivalent) was chosen mainly for the automatic cleanup, as the number of files can add up in cases where mutexes are not disposed, particularly when randomly generated names are used upon each invocation of a process. Due to the use of a global location/tmp/or equivalent, it would be possible for a user to deny access to the.dotnet-uidNdirectory of a different user if the directory hadn't been created yet. An alternative considered was to use the home directory and introduce some kind of periodic cleanup strategy, but there are also other tradeoffs./tmp/or equivalent must either have the sticky bit, or it must be owned by the current user and without write access for any other user. Otherwise, an exception is thrown./tmp/.dotnet-uidN/directory and files/directories under it are limited in access to the current userNamespaces
CurrentSessionOnly=trueis close, but it is possible for multiple users to be running code simultaneously in the same session. There is a global namespace, and a namespace per session. WhenCurrentUserOnly=true, callers may need to ensure that the name used is distinguished for different users.CurrentUserOnly=true, so each user has a separate namespace for objects, including for session-scoped and session-unscoped objects.