Bug 14407 - Major issues with SQLite Prepare2
Summary: Major issues with SQLite Prepare2
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.4.4
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-09-03 19:26 UTC by Neal
Modified: 2013-09-30 11:03 UTC (History)
3 users (show)

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

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 NOT_ON_ROADMAP

Description Neal 2013-09-03 19:26:50 UTC
Hello,

Probably similar to case 13594, my app is crashing frequently due to a runtime issue with this code in sqlite-net:

		[DllImport("sqlite3", EntryPoint = "sqlite3_prepare_v2", CallingConvention=CallingConvention.Cdecl)]
		public static extern Result Prepare2 (IntPtr db, [MarshalAs(UnmanagedType.LPStr)] string sql, int numBytes, out IntPtr stmt, IntPtr pzTail);

		public static IntPtr Prepare2 (IntPtr db, string query)
		{
			IntPtr stmt;
			var r = Prepare2 (db, query, query.Length, out stmt, IntPtr.Zero);
			if (r != Result.OK) {
				throw SQLiteException.New (r, GetErrmsg (db));
			}
			return stmt;
		}

Thread started:  #11
mono-rt: Stacktrace:


mono-rt:   at <unknown> <0xffffffff>

mono-rt:   at (wrapper managed-to-native) SQLite.SQLite3.Prepare2 (intptr,byte[],int,intptr&,intptr) <IL 0x0003d, 0xffffffff>

mono-rt:   at SQLite.SQLite3.Prepare2 (intptr,string) [0x00035] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/SQLite/SQLite.cs:2789

mono-rt:   at SQLite.SQLiteCommand.Prepare () [0x00012] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/SQLite/SQLite.cs:2025

mono-rt:   at SQLite.SQLiteCommand.ExecuteScalar<int> () [0x0002e] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/SQLite/SQLite.cs:1974

mono-rt:   at SQLite.TableQuery`1.Count () [0x0000c] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/SQLite/SQLite.cs:2664

mono-rt:   at LogbookPro.RatingTable.HasUnsynced () [0x00077] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/SQLite/RatingTable.cs:132

mono-rt:   at LogbookPro.DBFactory.get_HasUnsynced () [0x00023] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/Database/DBFactory.cs:239

mono-rt:   at LogbookPro.RootViewController.<ColorSyncButton>m__37 (object) [0x00014] in /Users/Neal/Dev/trunk/LogbookPro/Logbook_Pro/XIBs/RootViewController.xib.cs:169

mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <IL 0x00052, 0xffffffff>

mono-rt: 
Native stacktrace:


mono-rt: 
=================================================================
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.
=================================================================

Any suggestions?  I'm using Xam Studio 4.0.12 and I'm trying iOS 6.4.4.12 (beta channel) and Mono 3.2.1.

Thank you.
Comment 1 Sebastien Pouliot 2013-09-06 13:38:57 UTC
If you can create a small, self-contained, test case that shows issue I'll have a look at it asap.
Comment 2 Neal 2013-09-10 11:10:43 UTC
Sebastien,

I wish I could reproduce it.  It's amazing what using Crashlytics reveals and when Xamarin Studio brings back support for it I think we'll see more things like this needing attention.

Can you view this info on crashlytics?

http://crashes.to/s/f606bd84193

Maybe this will help you see what's going on?
Comment 3 Sebastien Pouliot 2013-09-10 11:41:10 UTC
> Can you view this info on crashlytics?

Yes, but it does not tell much but it shows that the crash occured on thread #18

> 0 ...	 libsqlite3.dylib	 sqlite3_log + 25457

The stack trace shows that sqlite tried to report (log) something. 

The device log would show that however that's not (AFAIK) part of what crashlytics includes in its report. That's why we need a test casesto reproduce it (and know what libsqlite is complaining about).

Also that's a dozen frames below managed code. If that code works (most of the time) then it's not likely to be a runtime/class library error.

> my app is crashing frequently

Since you can't reproduce it I assume it happens only on customer devices ? If so do you have other Crashlytics reports (links) ?

If so maybe there pattern (across threads) that might give us a clue.
Comment 4 Neal 2013-09-10 11:45:34 UTC
Thanks Sebastien,

I don't know that the app is crashing vs. this problem is a common one reported via Crashlytics.  It may just be "noise" and something I shouldn't worry about as I'm new to crashlytics.  It has been a good tool to help find things I wouldn't know about otherwise, but it may be detecting things outside of my/our control as well.
Comment 5 Sebastien Pouliot 2013-09-10 11:59:24 UTC
Just some additional (late) thoughs. The main thread was executing this:

NCSoftware_Common__NCSoftware_Common_Logger_LogException_string_System_Exception_string

1. Is that exception logged somewhere ?

2. Could it crash only when this execute ? i.e. is this a common path ?

e.g. could that code be closing some sqlite connection while another thread is still executing ?
Comment 6 Neal 2013-09-10 12:05:59 UTC
Yes, I do have a logger in the app but that user, which is unidentified in Crashlytics, would have to submit a bug report to me for me to get it.  Only if I hear from them can I get this information from them, however, crashlytics is reporting on the errors it detects.

I use sqlite-net, I don't handle the opening or closing of connections.  I used to have the source in the project, now I switched to the component in my recent 5.1 release so we'll see how things go, but I am still occasionally getting them.  

My concern is it appears to be a managed to native wrapper issue?
Comment 7 Sebastien Pouliot 2013-09-10 12:39:07 UTC
> My concern is it appears to be a managed to native wrapper issue?

No it's not, and the main thread is not crashing. 

OTOH I'm curious about what was done inside it, because it's freeing memory (which is totally fine) unless that include something that thread #18 requires.

a. Can you show me the source for `NCSoftware_Common__NCSoftware_Common_Logger_LogException_string_System_Exception_string` ? (you can mark that comment as private)

b. Can you try a fault injection ? i.e. doing something that ensure that code gets called while sqlite runs in another tread ? maybe that would allow you to duplicate the issue (and see what's in the device console log).
Comment 8 Neal 2013-09-10 12:48:10 UTC
Here is my logger class, nothing to hide:

using System;
using System.IO;

namespace NCSoftware.Common
{
	public class Logger
	{
		
		/* Standarize Logger Calls
		 */
		 
		public static void DebugLogMessage(string message,params object[] objects)
		{
#if DEBUG
			//WriteToLog(string.Format(System.Globalization.CultureInfo.InvariantCulture, "\n\n{0}\t{1}", DateTime.UtcNow, message));
			Console.WriteLine(message,objects);
#endif
		}
		public static void DebugLogMessage(string message)
		{
#if DEBUG
			//WriteToLog(string.Format(System.Globalization.CultureInfo.InvariantCulture, "\n\n{0}\t{1}", DateTime.UtcNow, message));
			Console.WriteLine(message);
#endif
		}
		
		public static void LogMessage(string message)
		{
			WriteToLog(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\t{1}", DateTime.UtcNow, message));
		}

		public static void LogException(string message, Exception ex, string methodName)
		{
			WriteToLog(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\tMessage: {1}\nException: {2}\nMethod: {3}", DateTime.UtcNow, message, ex.Message + ":::" + ex.StackTrace, methodName));
		}

		private static void WriteToLog(string msg)
		{
            lock (Helper.LogFilename)
            {
                using (var writer = File.AppendText(Helper.LogFilename))
                {
                    writer.Write(msg);
                    writer.Write("\n\n--------------------------------------------\n\n");
#if DEBUG
                    Console.WriteLine(msg); //view while debugging
#endif
                }
            }
		}
		
		/// <summary>
		/// Delete the log file.
		/// </summary>
		public static void Clear()
		{
			try
			{
				if(File.Exists(Helper.LogFilename))
					File.Delete(Helper.LogFilename);
			} catch
			{
			}	
		}
	}
}

I think too is I have to learn when to use lock statements properly, I may be doing this wrong.  Always learning!
Comment 9 Neal 2013-09-10 12:55:59 UTC
Lock statements with sqlite calls that is. Not sure if it's an issue but I think I need to review this usage.  Probably irrelevant to this case.
Comment 10 pravi.pravi 2013-09-30 11:03:37 UTC
When I am querying for Delete command , it throws same exception for me.

How we can delete Complete data from a table using delete command