dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.47k stars 4.76k forks source link

Named mutex not working on Linux and MacOS when AOT compiling #110348

Open darind opened 17 hours ago

darind commented 17 hours ago

Description

Acquiring a global named mutex doesn't seem to work on MacOS and Linux when the application is compiled with NativeAOT.

Reproduction Steps

internal static class Program
{
    private static void Main(string[] args)
    {
        using (var mutex = new Mutex(false, "Global\\{604074dc-be47-4a35-b79d-b79eead0c022}"))
        {
            mutex.WaitOne();

            Console.WriteLine("The app is now running...");
            Console.ReadLine();
        }
    }
}

Compile to native using the following:

dotnet publish --configuration Release --runtime osx-x64 --output dist

Expected behavior

The second instance of the program blocks and waits until the first has terminated

Actual behavior

The second instance successfully acquires the mutex.

Regression?

No response

Known Workarounds

No response

Configuration

Other information

The same works if the application is not compiled to native code but simply build in release mode:

dotnet build --configuration Release
dotnet-policy-service[bot] commented 17 hours ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

MichalStrehovsky commented 14 hours ago

I think this is #48720. Native AOT uses the managed Mutex implementation, not the one in CoreCLR PAL.