ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
512 stars 106 forks source link

AccessViolationException in sqlite3_finalize #588

Open pdupont1 opened 4 months ago

pdupont1 commented 4 months ago

I am getting occasional AccessViolationExceptions from sqlite3_finalize when running my file sync application. The application I am developing consists of several independent services that access a SQLite database to share state information.

As soon as the application starts to sync thousands of files, occasionally, it runs into the given exception which crashes the runtime. The provided stacktrace is from the Windows Event Viewer. Unfortunately I am not able to reliably reproduce this issue, but it only seems to appear in situations where multiple threads try to access the SQLite database very frequently.

What version of SQLitePCLRaw are you using?

Indirect dependency using Microsoft.Data.Sqlite (8.0.2)

If you are using one of the SQLitePCLRaw bundle packages, which one?

SQLitePCLRaw.bundle_e_sqlite3 (2.1.6)

What platform are you running on? What operating system? Which version? What CPU?

Windows 10 Pro (10.0.19045) 11th Gen Intel(R) Core(TM) i7-11850H

Are you on .NET Framework or the newer stuff (.NET Core, .NET 5+, etc)?

.NET Core 8.0.2

Are you using the command line, or an IDE? Which IDE? Which version of that IDE?

Microsoft Visual Studio Community 2022 (64-bit) Version 17.9.1

This is the full error information from the Windows Event Viewer (I have removed company specific parts):

Application: DesktopClient.Tray.exe
CoreCLR Version: 8.0.224.6711
.NET Version: 8.0.2
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack:
   at SQLitePCL.SQLite3Provider_e_sqlite3+NativeMethods.sqlite3_finalize(IntPtr)
   at SQLitePCL.SQLite3Provider_e_sqlite3+NativeMethods.sqlite3_finalize(IntPtr)
   at SQLitePCL.sqlite3_stmt.ReleaseHandle()
   at System.Runtime.InteropServices.SafeHandle.InternalRelease(Boolean)
   at Microsoft.Data.Sqlite.SqliteCommand.DisposePreparedStatements(Boolean)
   at Dapper.SqlMapper+<QueryRowAsync>d__34`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Dapper.SqlMapper+<QueryRowAsync>d__34`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]](<QueryRowAsync>d__34`1<System.__Canon> ByRef)
   at Dapper.SqlMapper.QueryRowAsync[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Data.IDbConnection, Row, System.Type, Dapper.CommandDefinition)
   at Dapper.SqlMapper.QueryFirstOrDefaultAsync[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Data.IDbConnection, Dapper.CommandDefinition)
   at <>.Persistence.SQLitePersistence+<>c__DisplayClass17_0`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<QueryFirstOrDefaultAsync>b__0(Microsoft.Data.Sqlite.SqliteConnection)
   at <>.Persistence.SQLitePersistence+<ExecuteOnConnectionAsync>d__20`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef)
   at <>.Persistence.SQLitePersistence.ExecuteOnConnectionAsync[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Func`2<Microsoft.Data.Sqlite.SqliteConnection,System.Threading.Tasks.Task`1<System.__Canon>>, Microsoft.Data.Sqlite.SqliteOpenMode)
   at <>.Persistence.SQLitePersistence.QueryFirstOrDefaultAsync[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.String, System.Object, System.Threading.CancellationToken)
   at <>.Persistence.Registry.RegistryPersistenceDb.LoadByIdAsync(System.String, System.Threading.CancellationToken)
   at <>.Service.Status.SyncCustomShellStatusService+<RefreshFileIconStatesAsync>d__20.MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult()
   at <>.Service.Status.SyncCustomShellStatusService+<UpdateFileIconStatusAsync>d__22.MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult()
   at <>.Service.Status.SyncCustomShellStatusService+<UpdateCustomPlaceholderFileStatesAsync>d__29.MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult()
   at <>.CloudFilterAPI.Shell.CustomPlaceholderState+<DoSetStatesAsync>d__9.MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.AsyncInfoToTaskBridge`2[[System.VoidValueTypeParameter, Microsoft.Windows.SDK.NET, Version=10.0.22621.30, Culture=neutral, PublicKeyToken=31bf3856ad364e35],[System.VoidValueTypeParameter, Microsoft.Windows.SDK.NET, Version=10.0.22621.30, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].Complete(Windows.Foundation.IAsyncInfo, System.Func`2<Windows.Foundation.IAsyncInfo,System.VoidValueTypeParameter>, Windows.Foundation.AsyncStatus)
   at ABI.Windows.Foundation.AsyncActionCompletedHandler.Do_Abi_Invoke(IntPtr, IntPtr, Windows.Foundation.AsyncStatus)
   at ABI.Windows.Storage.IStorageLibraryChangeTrackerMethods.Reset(WinRT.IObjectReference)
   at <>.FileSystem.Local.StorageTracker+<ResetInternalAsync>d__9.MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].SetExistingTaskResult(System.Threading.Tasks.Task`1<System.__Canon>, System.__Canon)
   at <>.FileSystem.Local.StorageTracker+<LoadTrackerAsync>d__12.MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.AsyncInfoToTaskBridge`2[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.VoidValueTypeParameter, Microsoft.Windows.SDK.NET, Version=10.0.22621.30, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].Complete(Windows.Foundation.IAsyncInfo, System.Func`2<Windows.Foundation.IAsyncInfo,System.__Canon>, Windows.Foundation.AsyncStatus)
   at ABI.Windows.Foundation.AsyncOperationCompletedHandler`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Do_Abi_Invoke(Void*, IntPtr, Windows.Foundation.AsyncStatus)

Faulting application name: DesktopClient.Tray.exe, version: 2.0.5.0, time stamp: 0x65a80000
Faulting module name: coreclr.dll, version: 8.0.224.6711, time stamp: 0x65a895d5
Exception code: 0xc0000005
Fault offset: 0x00000000001d1f48
Faulting process id: 0x737c
Faulting application start time: 0x01da65a14030232e
Faulting application path: <>.TestMSIX\bin\x64\Debug\AppX\DesktopClient.Tray\DesktopClient.Tray.exe
Faulting module path: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.2\coreclr.dll
Report Id: 6690b54f-22c8-4733-a237-1ac7d55d66c8

Are you using PackageReference or packages.config?

PackageReference

Sometimes other packages using SQLitePCLRaw cause problems when they are mixed together. What other packages are you including in your project?

<ItemGroup>
    <PackageReference Include="Dapper">
      <Version>2.1.28</Version>
    </PackageReference>
    <PackageReference Include="CDGrpcDotNetNamedPipes" Version="2.0.3" />
    <PackageReference Include="HtmlAgilityPack">
      <Version>1.11.59</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.2" />
    <PackageReference Include="Ninject">
      <Version>3.3.6</Version>
    </PackageReference>
    <PackageReference Include="RestSharp">
      <Version>110.2.0</Version>
    </PackageReference>
    <PackageReference Include="SharpCompress" Version="0.36.0" />
    <PackageReference Include="System.Linq.Async" Version="6.0.1" />
    <PackageReference Include="System.Memory">
      <Version>4.5.5</Version>
    </PackageReference>
    <PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
    <PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
    <PackageReference Include="System.ServiceModel.Primitives" Version="8.0.0" />
  </ItemGroup>
ericsink commented 4 months ago

That's going to be tricky to fix until we can find a repro.

For now I can confirm that this does look like a bug.

pdupont1 commented 1 month ago

It seems that this issue no longer occurs in 2.1.8. I am not able to run into the error on 2.1.8 whereas it does happen eventually at some point when rolling back to 2.1.6. Microsoft.Data.Sqlite (8.0.5) still uses 2.1.6, I am now explicitly referencing 2.1.8 as a workaround.

ericsink commented 1 month ago

This may have been one of the issues fixed in 2.1.7.