Bug 7134 - SIGSEV in Sqlite (Includes simplified code to reproduce.)
Summary: SIGSEV in Sqlite (Includes simplified code to reproduce.)
Status: RESOLVED DUPLICATE of bug 7667
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Database ()
Version: 3.0.x
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-09-13 08:00 UTC by bglsmith
Modified: 2012-10-19 06:52 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Code to recreate bug. 3 running threads synchronized in database access. (4.22 KB, application/octet-stream)
2012-09-13 08:04 UTC, bglsmith
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 7667

Description bglsmith 2012-09-13 08:00:49 UTC
Description of Problem:
Hard crash when using Sqlite in a multi-threaded environment.
Locking is used to guarantee single access to the database.
It seems to be related to transactions.

Steps to reproduce the problem:
1. Run the attached code.

Expected Results:

Stacktrace:

  at (wrapper managed-to-native) Mono.Data.Sqlite.UnsafeNativeMethods.sqlite3_prepare (intptr,intptr,int,intptr&,intptr&) <IL 0x0002a, 0xffffffff>
  at Mono.Data.Sqlite.SQLite3.Prepare (Mono.Data.Sqlite.SqliteConnection,string,Mono.Data.Sqlite.SqliteStatement,uint,string&) [0x00044] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLite3.cs:268
  at Mono.Data.Sqlite.SqliteCommand.BuildNextCommand () [0x00019] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:230
  at Mono.Data.Sqlite.SqliteCommand.GetStatement (int) [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:264
  at (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteCommand.GetStatement (int) <IL 0x00039, 0xffffffff>
  at Mono.Data.Sqlite.SqliteDataReader.NextResult () [0x000cc] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteDataReader.cs:896
  at Mono.Data.Sqlite.SqliteDataReader..ctor (Mono.Data.Sqlite.SqliteCommand,System.Data.CommandBehavior) [0x00051] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteDataReader.cs:89
  at (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteDataReader..ctor (Mono.Data.Sqlite.SqliteCommand,System.Data.CommandBehavior) <IL 0x00021, 0xffffffff>
  at Mono.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior) [0x00006] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:539
  at Mono.Data.Sqlite.SqliteCommand.ExecuteNonQuery () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:568
  at SqliteBug.AppDelegate.Run1 () [0x00060] in /Users/admin/Projects/SqliteBug/SqliteBug/AppDelegate.cs:110
  at System.Threading.Thread.StartInternal () [0x0001d] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/Thread.cs:697
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <IL 0x0004e, 0xffffffff>

Native stacktrace:

	0   SqliteBug                           0x000908cc mono_handle_native_sigsegv + 284
	1   SqliteBug                           0x000056f8 mono_sigsegv_signal_handler + 248
	2   libSystem.B.dylib                   0x999a705b _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   libsqlite3.dylib                    0x048adb7c sqlite3DbReallocOrFree + 28
	5   libsqlite3.dylib                    0x04904d37 sqlite3Insert + 6487
	6   libsqlite3.dylib                    0x04912a23 yy_reduce + 11187
	7   libsqlite3.dylib                    0x0491c214 sqlite3Parser + 244
	8   libsqlite3.dylib                    0x0491c482 sqlite3RunParser + 482
	9   libsqlite3.dylib                    0x0491d2db sqlite3Prepare + 683
	10  libsqlite3.dylib                    0x0491d8cb sqlite3LockAndPrepare + 299
	11  libsqlite3.dylib                    0x04901e95 sqlite3_prepare + 53
	12  ???                                 0x1234a81f 0x0 + 305440799
	13  ???                                 0x12349b50 0x0 + 305437520
	14  ???                                 0x12349691 0x0 + 305436305
	15  ???                                 0x123493c4 0x0 + 305435588
	16  ???                                 0x12349344 0x0 + 305435460
	17  ???                                 0x12348d24 0x0 + 305433892
	18  ???                                 0x12348876 0x0 + 305432694
	19  ???                                 0x12348710 0x0 + 305432336
	20  ???                                 0x12347b1c 0x0 + 305429276
	21  ???                                 0x123479c0 0x0 + 305428928
	22  ???                                 0x1236c8b7 0x0 + 305580215
	23  ???                                 0x12350c2e 0x0 + 305466414
	24  ???                                 0x0cacfb98 0x0 + 212663192
	25  SqliteBug                           0x00009ab2 mono_jit_runtime_invoke + 722
	26  SqliteBug                           0x0016b34e mono_runtime_invoke + 126
	27  SqliteBug                           0x0016b4bc mono_runtime_delegate_invoke + 140
	28  SqliteBug                           0x001a6bd2 start_wrapper + 466
	29  SqliteBug                           0x001d8b6a thread_start_routine + 154
	30  SqliteBug                           0x001fff5d GC_start_routine + 93
	31  libSystem.B.dylib                   0x9996e259 _pthread_start + 345
	32  libSystem.B.dylib                   0x9996e0de thread_start + 34

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

How often does this happen? 

It is related to garbage collection, so it seems somewhat random.
This particular code crashed about 10 seconds into run-time.
Comment 1 bglsmith 2012-09-13 08:04:24 UTC
Created attachment 2521 [details]
Code to recreate bug.  3 running threads synchronized in database access.

I'm not sure it attached the file before, so I'm reattaching it.
Comment 2 bglsmith 2012-09-13 08:28:22 UTC
I confirmed that this is not strictly related to transactions.

I've been running this test multiple times and it also brings out another common bug reported for Sqlite.

Sometimes the connection to create the database will lock the database, and the connections on the threads can't get access to it on their first command.  The connection to create the database is closed before any of the threads start running.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2012-10-19 06:52:37 UTC
Sqlite can't be used from multiple threads simultaneously in iOS by default .

You can change Sqlite default to be "more" thread-safe by calling, very early
in your application (i.e. before any other use of Sqlite) this code:

    SqliteConnection.SetConfig (SQLiteConfig.Serialized);

This will serialize all the calls, so it might not be as fast as the default
"SQLiteConfig.MultiThread", but it will ensure you won't run into threading
issues.

*** This bug has been marked as a duplicate of bug 7667 ***