question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] LiteDB 5 System.IO.IOException: The process cannot access the file because it is being used by another process

See original GitHub issue

We currently have a client application which consists of a Windows Service and a WPF application, both utilising LiteDB and targeting .Net Framework 4.8.

The windows service runs under the local system account and the WPF app runs under the user’s account.

We made the choice to upgrade from 4.1.4 to 5.0.7 in the hopes that concurrency was better supported.

Both WPF app and win service access the same LiteDB (read and write) with connection=shared using the below connection string:

"Filename=C:\temp\Test-v5.db;password=pass1234;connection=shared"

using (var db = new LiteDatabase(@"Filename=C:\temp\Test-v5.db;password=pass1234;connection=shared"))
{
	...
}

We seem to have come across a breaking issue however and can’t seem to find anyone else having the same/similar issue or anything else that could fix it.

The service frequently polls the database to handle and process data that has been inserted by the WPF app with both read and write operations. The WPF app also reads and writes to the database but on a more sporadic time frame based on user interaction.

Despite both applications accessing the database in the same way and both specifying connection=shared, we are getting exceptions from both as they appear to be clashing.

"System.IO.IOException: The process cannot access the file 'C:\temp\Test-v5.db' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at LiteDB.Engine.FileStreamFactory.GetStream(Boolean canWrite, Boolean sequencial)
   at LiteDB.Engine.StreamPool.<>c__DisplayClass3_0.<.ctor>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at LiteDB.Engine.DiskService..ctor(EngineSettings settings, Int32[] memorySegmentSizes)
   at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
   at LiteDB.SharedEngine.OpenDatabase()
   at LiteDB.SharedEngine.Query(String collection, Query query)
   at LiteDB.LiteQueryable`1.<ToDocuments>d__26.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Pixel_Upload_Service.UploadService.GetPendingJobsFromLiteDB(Boolean _allJobs) in D:\Users\Daniel\Desktop\LiteDB v5 Test\Upload Service\UploadService.cs:line 2654"

As a side note when testing this issue and trying to figure out what was going on, we found that we did not get any exceptions with two WPF apps reading and writing from the same database, however as soon as we swap one WPF app out for a Windows Service, we start getting exceptions.

Does anyone know how we can stop this happening?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

2reactions
lbnascimentocommented, Apr 29, 2020

@DanTwomey I made a commit that should fix the AbandonedMutexException problem, try it and see if it works for you. I’m going to close this issue, since the original issue was solved. If the problem persists if you have another problem, please open another issue.

1reaction
lbnascimentocommented, Apr 27, 2020

@DanTwomey Could you try with the latest master?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] Cannot access the file... it is being used by another ...
System.IO.IOException: The process cannot access the file 'C:\ProgramX\Tags.db' because it is being used by another process. at System.IO.
Read more >
Getting error System.IO.IOException: 'The process cannot ...
IOException : 'The process cannot access the file 'C:\Users\Soyuz\TestApp\bin\Debug\Soyuz.db' because it is being used by another process.'. This ...
Read more >
The process cannot access the file 'database.db' because it ...
The process cannot access the file 'C:\Users\matts\AppData\Local\UniversalAutomation\database.db' because it is being used by another process. I ...
Read more >
The process cannot access the file because it is being ...
I tried to update me db schema in my code and after changing that if i run my application i ma getting the...
Read more >
2.0.4.5 - The process cannot access the file because it is ...
IOException : The process cannot access the file because it is being used by another process.” because the database file is locked by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found