Skip to content
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

IOException when creating a global mutex on macOS #100135

Open
svick opened this issue Mar 22, 2024 · 4 comments
Open

IOException when creating a global mutex on macOS #100135

svick opened this issue Mar 22, 2024 · 4 comments

Comments

@svick
Copy link
Contributor

svick commented Mar 22, 2024

Description

We have a customer that reported getting the following exception when our code is creating a global named Mutex on macOS (see postsharp/Metalama#272):

Unhandled exception. System.IO.IOException: The system cannot open the device or file specified. : 'Global\Metalama.Configuration'
   at System.Threading.Mutex.CreateMutexCore(Boolean initiallyOwned, String name, Boolean& createdNew)
   at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)

The exception happened consistently, but did not reoccur after restarting the machine, so it seems it's caused by some corrupted temporary state.

What could have caused this situation? Could it be a bug in our code, or a bug in the macOS implementation of Mutex? What should we do to avoid this problem in the future?

Reproduction Steps

I do not have a reproduction. The relevant code on our side is here, but it boils down to:

internal static Mutex OpenOrCreateMutex( string mutexName )
{
    // The number of iterations is intentionally very low.
    // We will restart if the following occurs:
    //   1) TryOpenExisting fails, i.e. there is no existing mutex.
    //   2) Creating a new mutex fails, i.e. the mutex was created in the meantime by a process with higher set of rights.
    // The probability of mutex being destroyed when we call TryOpenExisting again is fairly low.

    for ( var i = 0; ; i++ )
    {
        // First try opening the mutex.
        if ( Mutex.TryOpenExisting( mutexName, out var existingMutex ) )
        {
            return existingMutex;
        }
        else
        {
            // Otherwise we will try to create the mutex.
            try
            {
                return new Mutex( false, mutexName );
            }
            catch ( UnauthorizedAccessException )
            {
                if ( i < 3 )
                {
                    // Mutex was probably created in the meantime and is not accessible - we will restart.
                    continue;
                }
                else
                {
                    // There were too many restarts - just rethrow.
                    throw;
                }
            }
        }
    }
}

Expected behavior

Either the exception is not thrown at all, or there is more information on the actual cause of the exception.

Actual behavior

The exception described above is thrown.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 22, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 22, 2024
@filipnavara
Copy link
Member

I'm not sure whether this is actionable without more information. What version of framework was used to run the app? I went all the way back to .NET 6 and the CreateMutexCore would not throw IOException there AFAICT.

@svick
Copy link
Contributor Author

svick commented Mar 22, 2024

@filipnavara Unfortunately, I don't have more information, apart from it being .Net 6+.

Though that exception message is associated with ERROR_OPEN_FAILED in the PAL, so I'm assuming that the exception is coming from native code.

@jkotas jkotas added area-System.Threading and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 22, 2024
@craigbehnke
Copy link

Hi @filipnavara, I'm the guy who opened the original issue. My app's target framework is net7.0 (installed SDKs are 7.0.311 and 7.0.313).

Please let me know what other info you would like.

@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label May 2, 2024
@mangod9 mangod9 added this to the 9.0.0 milestone May 2, 2024
@mangod9 mangod9 modified the milestones: 9.0.0, 8.0.x, Future Jul 3, 2024
@albahari
Copy link

albahari commented Dec 5, 2024

I have experience this, too, on .NET 8.0.6 on macOS. Same stack trace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants