System.Data.SQLite
View Ticket
Not logged in
Ticket UUID: d0d1d444d3220c7e74039b6c11236d88fc492eee
Title: Deadlocks with SQLite Pooling in multithread.
Status: Closed Type: Incident
Severity: Important Priority: Medium
Subsystem: Connection_Pool Resolution: Works_As_Designed
Last Modified: 2014-08-15 15:10:13
Version Found In: 1.0.93.0
User Comments:
anonymous added on 2014-08-13 11:42:45:

I use SQLite driver in a Game Server C# development which use async socket connection and may happen to trigger SQL queries out of the main thread when handling player packets...

   at System.GC._WaitForPendingFinalizers in   line:0 col:0
   at System.Data.SQLite.SQLiteConnectionPool.Remove in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteConnectionPool.cs  line:214 col:0
   at System.Data.SQLite.SQLite3.Open in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLite3.cs  line:335 col:0
   at System.Data.SQLite.SQLiteConnection.Open in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteConnection.cs  line:1686 col:0
   at DOL.Database.Connection.DataConnection.ExecuteSelect in d:\work\freyad\trunk\DOLDatabase\Connection\DataConnection.cs  line:377 col:12
   at DOL.Database.Handlers.SQLiteObjectDatabase.SelectObjectsImpl in d:\work\freyad\trunk\DOLDatabase\Handlers\SQLiteObjectDatabase.cs  line:810 col:4
   at DOL.Database.ObjectDatabase.SelectObjects in d:\work\freyad\trunk\DOLDatabase\ObjectDatabase.cs  line:324 col:4
   at DOL.Database.ObjectDatabase.SelectObjects in d:\work\freyad\trunk\DOLDatabase\ObjectDatabase.cs  line:318 col:4
   at DOL.GS.ServerRules.AbstractServerRules.IsAllowedToConnect in d:\work\freyad\trunk\GameServer\serverrules\AbstractServerRules.cs  line:96 col:5
   at DOL.GS.PacketHandler.Client.v168.LoginRequestHandler.HandlePacket in d:\work\freyad\trunk\GameServer\packets\Client\168\LoginRequestHandler.cs  line:153 col:5
   at DOL.GS.PacketHandler.PacketProcessor.HandlePacket in d:\work\freyad\trunk\GameServer\packets\Server\PacketProcessor.cs  line:965 col:6
   at DOL.GS.PacketHandler.PacketProcessor.ReceiveBytes in d:\work\freyad\trunk\GameServer\packets\Server\PacketProcessor.cs  line:741 col:7
   at DOL.GS.GameClient.OnReceive in d:\work\freyad\trunk\GameServer\GameClient.cs  line:561 col:5
   at DOL.Network.BaseClient.OnReceiveHandler in d:\work\freyad\trunk\DOLBase\Network\BaseClient.cs  line:207 col:6
   at System.Net.LazyAsyncResult.Complete in   line:0 col:0
   at System.Net.ContextAwareResult.CompleteCallback in   line:0 col:0
   at System.Threading.ExecutionContext.RunInternal in   line:0 col:0
   at System.Threading.ExecutionContext.Run in   line:0 col:0
   at System.Threading.ExecutionContext.Run in   line:0 col:0
   at System.Net.ContextAwareResult.Complete in   line:0 col:0
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback in   line:0 col:0
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback in   line:0 col:0
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback in   line:0 col:0

This works fine when Pooling is disable, but when pooling is enable it seems the call to "WaitForPendingFinalizers()" hangs forever. I guess it's some code that run for cleaning the Connection Pool.

There is some comments around this in source :

// BUGFIX: For ticket [996d13cd87], step #2.  Now,
//         we must wait for all pending finalizers
//         which have STARTED running and have not
//         yet COMPLETED.  This must be done just
//         in case the finalizer for the connection
//         handle just obtained from the queue has
//         STARTED running at some point before
//         SuppressFinalize was called on it.

Maybe there is some other way to prevent the described racing condition ? or Pooling should really not be used in this kind of lightly threaded environment ?


mistachkin added on 2014-08-13 13:52:02:
Are any of your IDisposable objects running their finalizers during this call?
Simply running System.Data.SQLite with connection pooling enabled on multiple
threads should not result in a deadlock.

mistachkin added on 2014-08-13 13:54:42:
This is most likely related to [393d954be0].