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

[BUG] iterating over an IEnumerable<LiteFileInfo<TFileId>> errors out on the second element if the DB is opened in shared mode #2534

Open
ssteiner opened this issue Aug 9, 2024 · 1 comment
Labels

Comments

@ssteiner
Copy link

ssteiner commented Aug 9, 2024

For a test, I changed the connection mode for LiteDb from exclusive to shared. And then, operations on files started erroring out.

the code I'm using looks like this:

var fs = db.GetStorage<string>("audioFiles");
var files = fs.Find(u => u.Id.StartsWith(path));
foreach (var file in files)
{ // do something with file
}

this works fine if the connection mode is direct. If you then change the connection mode to shared, It fails when there's multiple files (at least two) - the first iteration works, on the second iteration gets you a LiteException with message

This engine instance already disposed

If you get a List instead of an Enumerable (var files = fs.Find(u => u.Id.StartsWith(path)).ToList();) it works even in shared mode.

I would expect the iteration to work in shared mode, too.

LiteDB version is 5.0.21 on .NET 8.0.7 on W11 Pro 2023H2

@ssteiner ssteiner added the bug label Aug 9, 2024
@Joy-less
Copy link

I can confirm, this is a really intriguing issue.

The following code gives you an error:

using LiteDatabase Database = new(new ConnectionString() {
    Filename = "Database.db",
    Connection = ConnectionType.Shared,
});
ILiteCollection<BsonDocument> Accounts = Database.GetCollection("Accounts");
foreach (BsonDocument Document in Accounts.FindAll()) {
    Accounts.Update(Document);
}
GD.Print("done");
void LiteDB.Engine.EngineState.Validate(): LiteDB.LiteException: This engine instance already disposed.

But you can remove the error by doing any of the following:

  • Replace Accounts.FindAll() with Accounts.FindAll().ToList()
  • Replace Connection = ConnectionType.Shared with Connection = ConnectionType.Direct
  • Remove Accounts.Update(Document);

Joy-less added a commit to Joy-less/LiteDB that referenced this issue Oct 4, 2024
Joy-less added a commit to Joy-less/LiteDB that referenced this issue Oct 4, 2024
@Joy-less Joy-less mentioned this issue Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants