-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include info about system call errors in some exceptions from operating on named mutexes #92603
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c11ecef
Include info about system call errors in some exceptions from operati…
kouvel a79ef63
Small adjustment
kouvel 5a551b9
Address feedback
kouvel 568e137
Undo a couple more things
kouvel 92a5fcb
Address feedback
kouvel bbdde59
Remove some Unix ifdefs
kouvel 5c37c7c
Address feedback
kouvel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/coreclr/System.Private.CoreLib/src/System/Threading/Mutex.CoreCLR.Unix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
namespace System.Threading | ||
{ | ||
/// <summary> | ||
/// Synchronization primitive that can also be used for interprocess synchronization | ||
/// </summary> | ||
public sealed partial class Mutex : WaitHandle | ||
{ | ||
private void CreateMutexCore(bool initiallyOwned, string? name, out bool createdNew) | ||
{ | ||
SafeWaitHandle mutexHandle = CreateMutexCore(initiallyOwned, name, out int errorCode, out string? errorDetails); | ||
if (mutexHandle.IsInvalid) | ||
{ | ||
mutexHandle.SetHandleAsInvalid(); | ||
if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE) | ||
// On Unix, length validation is done by CoreCLR's PAL after converting to utf-8 | ||
throw new ArgumentException(SR.Argument_WaitHandleNameTooLong, nameof(name)); | ||
if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE) | ||
throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name)); | ||
|
||
throw Win32Marshal.GetExceptionForWin32Error(errorCode, name, errorDetails); | ||
} | ||
|
||
createdNew = errorCode != Interop.Errors.ERROR_ALREADY_EXISTS; | ||
SafeWaitHandle = mutexHandle; | ||
} | ||
|
||
private static OpenExistingResult OpenExistingWorker(string name, out Mutex? result) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(name); | ||
|
||
result = null; | ||
// To allow users to view & edit the ACL's, call OpenMutex | ||
// with parameters to allow us to view & edit the ACL. This will | ||
// fail if we don't have permission to view or edit the ACL's. | ||
// If that happens, ask for less permissions. | ||
SafeWaitHandle myHandle = OpenMutexCore(name, out int errorCode, out string? errorDetails); | ||
|
||
if (myHandle.IsInvalid) | ||
{ | ||
myHandle.Dispose(); | ||
|
||
if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE) | ||
{ | ||
// On Unix, length validation is done by CoreCLR's PAL after converting to utf-8 | ||
throw new ArgumentException(SR.Argument_WaitHandleNameTooLong, nameof(name)); | ||
} | ||
if (Interop.Errors.ERROR_FILE_NOT_FOUND == errorCode || Interop.Errors.ERROR_INVALID_NAME == errorCode) | ||
return OpenExistingResult.NameNotFound; | ||
if (Interop.Errors.ERROR_PATH_NOT_FOUND == errorCode) | ||
return OpenExistingResult.PathNotFound; | ||
if (Interop.Errors.ERROR_INVALID_HANDLE == errorCode) | ||
return OpenExistingResult.NameInvalid; | ||
|
||
throw Win32Marshal.GetExceptionForWin32Error(errorCode, name, errorDetails); | ||
} | ||
|
||
result = new Mutex(myHandle); | ||
return OpenExistingResult.Success; | ||
} | ||
|
||
// Note: To call ReleaseMutex, you must have an ACL granting you | ||
// MUTEX_MODIFY_STATE rights (0x0001). The other interesting value | ||
// in a Mutex's ACL is MUTEX_ALL_ACCESS (0x1F0001). | ||
public void ReleaseMutex() | ||
{ | ||
if (!Interop.Kernel32.ReleaseMutex(SafeWaitHandle)) | ||
{ | ||
throw new ApplicationException(SR.Arg_SynchronizationLockException); | ||
} | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
// Unix-specific implementation | ||
|
||
private const int SystemCallErrorsBufferSize = 256; | ||
|
||
private static unsafe SafeWaitHandle CreateMutexCore( | ||
bool initialOwner, | ||
string? name, | ||
out int errorCode, | ||
out string? errorDetails) | ||
{ | ||
byte* systemCallErrors = stackalloc byte[SystemCallErrorsBufferSize]; | ||
SafeWaitHandle mutexHandle = CreateMutex(initialOwner, name, systemCallErrors, SystemCallErrorsBufferSize); | ||
|
||
// Get the error code even if the handle is valid, as it could be ERROR_ALREADY_EXISTS, indicating that the mutex | ||
// already exists and was opened | ||
errorCode = Marshal.GetLastPInvokeError(); | ||
|
||
errorDetails = mutexHandle.IsInvalid ? GetErrorDetails(systemCallErrors) : null; | ||
return mutexHandle; | ||
} | ||
|
||
private static unsafe SafeWaitHandle OpenMutexCore(string name, out int errorCode, out string? errorDetails) | ||
{ | ||
byte* systemCallErrors = stackalloc byte[SystemCallErrorsBufferSize]; | ||
SafeWaitHandle mutexHandle = OpenMutex(name, systemCallErrors, SystemCallErrorsBufferSize); | ||
errorCode = mutexHandle.IsInvalid ? Marshal.GetLastPInvokeError() : Interop.Errors.ERROR_SUCCESS; | ||
errorDetails = mutexHandle.IsInvalid ? GetErrorDetails(systemCallErrors) : null; | ||
return mutexHandle; | ||
} | ||
|
||
private static unsafe string? GetErrorDetails(byte* systemCallErrors) | ||
{ | ||
int systemCallErrorsLength = | ||
new ReadOnlySpan<byte>(systemCallErrors, SystemCallErrorsBufferSize).IndexOf((byte)'\0'); | ||
if (systemCallErrorsLength > 0) | ||
{ | ||
try | ||
{ | ||
return | ||
SR.Format(SR.Unix_SystemCallErrors, Encoding.UTF8.GetString(systemCallErrors, systemCallErrorsLength)); | ||
} | ||
catch { } // avoid hiding the original error due to an error here | ||
} | ||
|
||
return null; | ||
} | ||
|
||
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "PAL_CreateMutexW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] | ||
private static unsafe partial SafeWaitHandle CreateMutex([MarshalAs(UnmanagedType.Bool)] bool initialOwner, string? name, byte* systemCallErrors, uint systemCallErrorsBufferSize); | ||
|
||
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "PAL_OpenMutexW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] | ||
private static unsafe partial SafeWaitHandle OpenMutex(string name, byte* systemCallErrors, uint systemCallErrorsBufferSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,3 +212,5 @@ class StringHolder | |
|
||
}; | ||
#endif /* _PAL_UTILS_H_ */ | ||
|
||
const char *GetFriendlyErrorCodeString(int errorCode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is error code ever worth getting / exposting from releasemutex? since you're preserving errors in other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Natural errors aren't expected from
ReleaseMutex
. There can be errors from acquiring the lock (waiting on a mutex) and that's not covered, but most of the issues are likely to be found when creating/opening a mutex. Covering waits is more involved.