You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Named mutex not working on Linux and MacOS when AOT compiling #110348
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();
}
}
}
The implementation used by NativeAOT currently just uses in-process mutexes and doesn't support cross-process synchronization. Fixing that would be good and is under consideration.
If you're using a named mutex to limit an application to one instance, an alternative may be to use the file system. Does your scenario need some of the other functionality of mutexes?
The implementation used by NativeAOT currently just uses in-process mutexes and doesn't support cross-process synchronization. Fixing that would be good and is under consideration.
If you're using a named mutex to limit an application to one instance, an alternative may be to use the file system. Does your scenario need some of the other functionality of mutexes?
The issue with using the file system is that if the application crashes, it can lead to a deadlock until the user manually deletes the file...
Some cross-platform details in dotnet always present problems, such as cross-platform compilation with Native AOT, global mutexes, and so on...
@scgm0 this works for me on Linux, Mac and Windows:
try{_lockFile=File.Open(LockFilePath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);}catch(Exceptione){Log.Information("Another Instance is already Running");Environment.Exit(1);}
If the application crashes and the lockfile doesn't get removed, the exclusive lock can be acquired on restart.
Activity
dotnet-policy-service commentedon Dec 3, 2024
Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.
MichalStrehovsky commentedon Dec 3, 2024
I think this is #48720. Native AOT uses the managed Mutex implementation, not the one in CoreCLR PAL.
darinkes commentedon Jan 21, 2025
What's the state of this issue?
Are there any known workarounds?
Just tried .net 9 and Mutex still doesn't work as expected if compiled with AOT.
Move away from named mutex due to issue on Linux/MacOS and use file b…
Move away from named mutex due to issue on Linux/MacOS and use file b…
Move away from named mutex due to issue on Linux/MacOS and use file b…
Move away from named mutex due to issue on Linux/MacOS and use file b…
Move away from named mutex due to issue on Linux/MacOS and use file b…
kouvel commentedon Feb 25, 2025
The implementation used by NativeAOT currently just uses in-process mutexes and doesn't support cross-process synchronization. Fixing that would be good and is under consideration.
If you're using a named mutex to limit an application to one instance, an alternative may be to use the file system. Does your scenario need some of the other functionality of mutexes?
scgm0 commentedon Mar 5, 2025
The issue with using the file system is that if the application crashes, it can lead to a deadlock until the user manually deletes the file...
Some cross-platform details in dotnet always present problems, such as cross-platform compilation with Native AOT, global mutexes, and so on...
darinkes commentedon Mar 5, 2025
@scgm0 this works for me on Linux, Mac and Windows:
If the application crashes and the lockfile doesn't get removed, the exclusive lock can be acquired on restart.
scgm0 commentedon Mar 5, 2025
Thanks for the approach! I'll see if it's available later
jkotas commentedon Jun 29, 2025
Duplicate of #48720