Occasional NullReferenceException when disposing
Closed this issue · 3 comments
I sometimes - not always, get a NullRef when disposing:
System.NullReferenceException : Object reference not set to an instance of an object.
at Microsoft.Data.Sqlite.SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText)
at Microsoft.Data.Sqlite.SqliteTransaction.RollbackInternal()
at Microsoft.Data.Sqlite.SqliteTransaction.Dispose(Boolean disposing)
at System.Data.Common.DbTransaction.Dispose()
at Microsoft.Data.Sqlite.SqliteConnection.Close()
at Microsoft.Data.Sqlite.SqliteConnection.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
using version 2.0
There's no obvious way of getting this exception from this code path. You may be calling Dispose()
on two different threads. If you can provide us a simple repro, we can investigate further.
I've got this, too, on my system test. It ran well for many times and then I got NullReferenceException on closing the connection.
public class QlcContext : DbContext
{
// ..
private SqliteConnection connection;
public override void Dispose()
{
base.Dispose();
connection.Close(); // NullReferenceException
}
protected override void OnConfiguring(DbContextOptionsBuilder contextOptionsBuilder)
{
connection = new SqliteConnection("Data Source=qlc.db");
connection.Open();
string password = "MyPass";
var command = connection.CreateCommand();
command.CommandText = "SELECT quote($password);";
command.Parameters.AddWithValue("$password", password);
var quotedPassword = (string)command.ExecuteScalar();
command.CommandText = "PRAGMA key = " + quotedPassword;
command.Parameters.Clear();
command.ExecuteNonQuery();
contextOptionsBuilder.UseSqlite(connection);
}
}
My used packages:
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
<PackageReference Include="SQLitePCLRaw.bundle_sqlcipher" Version="1.1.9" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="1.1.9" ExcludeAssets="All" />