LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG] LiteDB will return null for DB entries by the very first access

Open michtoen opened this issue 1 year ago • 1 comments

Version LiteDb 5.0.21 on .net 8 (linked in by standard 2.0 dll)

Describe the bug When we start our app with liteDB 5.0.21, it will access a row of liteDBs. In 50% of the INITIAL access to that DB it will fail to initialize the return for some fields of a dictionary with the right values but set them to null. Any access after the first one will return the right values, always.

The data is simple:


class FileData
{
    public string Path { get; set; } 
    public string Key  { get; set; }
}

class _fileDataDic
{
    public string id { get; set; }
    public Dictionary<string, FileData> dic { get; set; }           
}

// The DB is created with 
Dictionary<string, FileData> entry = new() {"foo.jpg", new() {Path = 'ABC', Key = "123"}};

 var col = db.GetCollection<_fileDataDic>("Example");
 col.Insert("FOO", new _fileDataDic { id = "FOO", dic =  entry});

// We access by this:
var col = db.GetCollection<_fileDataDic>("Example");
_fileDataDic fileDataDic = col.FindById("FOO");

In 50% now the returned fileDataDic will look like this

{
  "id": "FOO",
  "dic": {
    "foo.jpg": {
      "Path": null,
      "Key": null
    }
  }
}

Path & Key are NOT null in the DB. A second request will return the right values for Path & Key

  • the effect will occur always when we start the app to around 50% off all DBs initial accesses
  • there is ATM always only one entry in the dictionary
  • it will vanish with the 2nd access
  • it never happens and works flawless in 5.0.20 and lower
  • we have build the (original 5.0.20) DB with 5.0.21 but the effect is the same - it is not the DB
  • we have not hard tested deeper but it seems to work after it

The LiteDB will be called by a Microsoft.AspNetCore.Mvc Controller.

michtoen avatar Aug 01 '24 12:08 michtoen

Here is an Examlpe Repo cleary showing the problem.

https://github.com/dos-ise/LiteDB_ThreadSafetyTest

dos-ise avatar Jan 08 '25 15:01 dos-ise