mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.57k stars 1.25k forks source link

[BUG] Getting SIGSEGV while reading data from Litedb collection simultaneously in multiple threads #1627

Open sanjiv-saini opened 4 years ago

sanjiv-saini commented 4 years ago

Version LiteDB version: 4.1.4 OS: Android API 27

Describe the bug I am using Litedb v4 in Xamarin, I am creating liteDatabase connection and getting collections inside it once, and passing the collection to multiple threads which perform givenCollection.findALL().ToList() simultaneously. The app crashes sometimes(not always) resulting in SIGSEGV error, I am not getting what is causing this error.

Code to Reproduce

protected async virtual Task<IEnumerable<T>> ReadAllItems<T>(LiteCollection<T> collection)
{
    return await Task.Run(() =>
    {
          return collection.FindAll().ToList();
     });
}

get the collection objection once and pass to the above method. calling above method multiple times causing the error.

Stacktrace

  at <unknown> <0xffffffff>
      at System.String:FastAllocateString <0x00037>
      at System.String:CreateStringFromEncoding <0x00
0fb>
      at System.Text.UTF8Encoding:GetString <0x002a3>
      at LiteDB.BsonReader:ReadString <0x00143>
      at LiteDB.BsonReader:ReadElement <0x00227>
      at LiteDB.BsonReader:ReadDocument <0x0015b>
      at LiteDB.BsonReader:ReadElement <0x002af>
      at LiteDB.BsonReader:ReadDocument <0x0015b>
      at LiteDB.BsonReader:ReadElement <0x002af>
      at LiteDB.BsonReader:ReadArray <0x0015b>
      at LiteDB.BsonReader:ReadElement <0x0031f>
      at LiteDB.BsonReader:ReadDocument <0x0015b>
      at LiteDB.BsonReader:ReadElement <0x002af>
      at LiteDB.BsonReader:ReadDocument <0x0015b>
      at LiteDB.BsonReader:Deserialize <0x0009b>
      at LiteDB.QueryCursor:Fetch <0x003eb>
      at <Find>d__9:MoveNext <0x007a3>
      at <Find>d__13:MoveNext <0x00e63>
      at <Find>d__17:MoveNext <0x00593>
      at System.Collections.Generic.List`1:AddEnumerable <0x00287>
      at System.Collections.Generic.List`1:.ctor <0x0038f>
      at <>c__DisplayClass7_0`1:<ReadAllItems>b__0 <0x0013b>
      at System.Threading.Tasks.Task`1:InnerInvoke <0x000d7>
      at System.Threading.Tasks04-11 02:13:11.211 F/libc    (13977): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x1b in tid 14021 (Thread Pool Wor)
lbnascimento commented 4 years ago

@sanju-singh Both LiteCollection and LiteDatabase are thread-safe in v4 and can be shared between multiple threads. Your problem seems to be a Mono-specific issue - I don't believe it is even possible to get a segmentation fault in managed code.

OneCyrus commented 4 years ago

litedb v4 is not thread safe. i made a PR to fix this issue but it's not merged yet. @lbnascimento any chance to get this released?

https://github.com/mbdavid/LiteDB/pull/1609