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

Replace Mutex name hashing with URI escaping (Fix #2543) #2557

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Joy-less
Copy link
Contributor

Shared mode uses a named Mutex to avoid multiple processes reading/writing at the same time. LiteDB names the Mutex to contain the absolute file path of the database. But Mutex names have to be valid file paths, so this would cause an invalid file path like:

Global\c:\users\user\documents\github\litedb\litedb.tests\bin\debug\net8\demo.db.Mutex

So instead, currently it hashes the file path using SHA1 to turn it into the following:

Global\0a1ab52c4b3f3d01730f61b59e7891bc029ab372.Mutex

However, this is a hacky solution in my opinion. Someone experienced exceptions (#2543) caused when creating SHA1. This PR avoids the need to hash the path by using URL escaping:

Global\c%3A%5Cusers%5Cuser%5Cdocuments%5Cgithub%5Clitedb%5Clitedb.tests%5Cbin%5Cdebug%5Cnet8%5Cdemo.db.Mutex

Also, I changed it to use .ToLowerInvariant() instead of .ToLower() because Windows file paths are case insensitive even for non-ASCII characters (e.g. É and é).

This PR should fix #2543 since it removes hashing.

@Joy-less
Copy link
Contributor Author

I should just mention that since this changes the name of the Mutex, shared mode won't lock correctly if you have two processes where one is using the current or older version of LiteDB and one is using a future LiteDB version.

@JKamsker
Copy link
Collaborator

Super hard to accept prs that are hard to test :/

@Joy-less
Copy link
Contributor Author

Super hard to accept prs that are hard to test :/

I don't think so... It's pretty simple. If you like, I can write a test to ensure two connections can still be active at once.

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.

[BUG]
2 participants