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

Changes to properly support Mutexes on NET6.0 #2206

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

conradmicallef
Copy link

Global Mutexes were being created without insufficient rights for a 2nd process running on same machine to use them

This fix addresses that by using same methodology applied to NETFRAMEWORK but to NET6.0

Global Mutexes were being created without insufficient rights for a 2nd process running on same machine to use them
@inbarbarkai
Copy link

Is there any plan on merging this PR?

@tjmoore
Copy link

tjmoore commented Oct 20, 2023

Any update? What's the symptom without this fix, is there a particular error raised?

Looking for solutions to access denied errors on shared mode access, but I'm not seeing it on the mutex but on the journal file (and only one process is writing, the other opens database in read mode).

@JKamsker
Copy link
Collaborator

JKamsker commented Jun 4, 2024

I would also like to see some repro code before merging this.

@inbarbarkai
Copy link

@JKamsker I can't provide a repro, but I can provide the scenario.

Environment: Windows 10/11
Version: 5.0.12

2 writer processes from different users.
1 of the writers is also a reader.

This happened sometimes during our tests, but our customers encountered it all the time, forcing us to release a patch disabling some functionalities.
We replaced it with SQLite to restore those functionalities.

@JKamsker
Copy link
Collaborator

JKamsker commented Jun 5, 2024

@inbarbarkai Oh i remember now, i had a similar problem with named pipes and different users.

Ok thank you for the clarification! I will try to get to it asap :)

@JKamsker JKamsker self-assigned this Jun 5, 2024
@tjmoore
Copy link

tjmoore commented Jun 5, 2024

Similar to above, have not reproduced the issues we see, but have had them from customers, particularly this kind of error.

System.IO.IOException: The process cannot access the file 'C:\\XXX\\somedatabase.db' because it is being used by another process.

This occurs either on .db or the log, and the processes all open the database in shared mode. Have tried long and short lived instances of LiteDatabase also (I believe in shared mode it is internally opening and closing connections on each operation so likely makes little difference, unlike direct mode).

Could this occur if the mutexes aren't being acquired right? Though would you see a mutex error instead? Or is there a potential race condition somewhere?

We've done extensive diagnostics and cannot see what the other process is. Some customers show signs of things like Microsoft Defender for Endpoints (the corporate version of Defender) access files but looks more like it detecting file access not actually opening and locking files. Other customers don't have anything like this though.

Any solution needs to be proven fix to roll out to customers though as already got frustrated customers, hence repo is very desirable.

Only alternative have thought about is to move to a single process for database operations and some communication between them. Getting into building a database server territory though and already considering supporting MongoDB as an alternative to LiteDB.

Comment on lines 30 to 59
#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
#if NET6_0_OR_GREATER
if (!OperatingSystem.IsWindows())
_mutex = new Mutex(false, "Global\\" + name + ".Mutex");
else
{
#endif
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);

var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);

var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
#if NET6_0_OR_GREATER
_mutex = MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings);
#endif
#if NETFRAMEWORK
_mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings);
#endif
#if NETSTANDARD2_0_OR_GREATER
_mutex = new Mutex(false, "Global\\" + name + ".Mutex");
ThreadingAclExtensions.SetAccessControl(_mutex, securitySettings);
#endif
#else
_mutex = new Mutex(false, "Global\\" + name + ".Mutex");
#endif
#if NET6_0_OR_GREATER
}
#endif
}
catch (NotSupportedException ex)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big nope! Please make it more readable.

<AssemblyVersion>5.0.12</AssemblyVersion>
<FileVersion>5.0.12</FileVersion>
<VersionPrefix>5.0.12</VersionPrefix>
<TargetFrameworks>netstandard1.3;netstandard2.0;net6.0</TargetFrameworks>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to add or drop support for any TargetFrameworks just yet ;)

@JKamsker
Copy link
Collaborator

JKamsker commented Jun 7, 2024

I am not sure about the added dependencies. What do you think @pictos
about the framework change: we are currently internally thinking about following the official microsoft support matrix.

@jobrien9
Copy link

Hi all - Is there any update regarding following the Microsoft support matrix? I'd love to add .NET 8 support if that's allowed. I was able to get this working locally for my .NET 8 applications using the code from this related issue, but I would be happy to help get this merged if I can.

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

Successfully merging this pull request may close these issues.

5 participants