Search code examples
c#wpfwindows-serviceslitedb

LiteDB 5 System.IO.IOException: The process cannot access the file


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?


Solution

  • This was a bug with LiteDB itself and has since been patched.

    The issue on github can be found here: https://github.com/mbdavid/LiteDB/issues/1656