Malformed databases and multithreading

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Malformed databases and multithreading

Paul Egli
Main question/comment:
-----

On the "how to corrupt" page ( http://sqlite.org/howtocorrupt.html ) i
do not see any mention of using SQLite in an incorrect way with
respect to thread safety. Is there really no way that, for example,
using the same connection on multiple threads at the same time could
either (a) corrupt the database, or (b) lead to a false reports of
corruption?

(Even if such a misuse of SQLite falls under one of the categories
already listed on the page, perhaps said category could be updated to
explicitly tell about the risk of misusing SQLite in this way...
perhaps a "Section 2.6"... or 5.1 or 7.1).

Thoughts?

-P



P.S./Appendix: Further background info (probably TMI)...
-----

We are trying to help a client debug some issues in a Xamarin-based
app that is deployed to both Android and iOS. Some of the errors they
are seeing are "malformed database".

They are still building in a way that uses the platform-provided
SQLite on both platforms (no, this will not work on Android N) and no
additional SQLite library is linked in, so they should not be having
the "Multiple copies of SQLite linked into the same application"
problem.

They also routinely see "cannot start a transaction within a
transaction" even though there is no obvious place where or reason why
that would happen in their code. This leads us to believe they may be
using the same connection on multiple threads.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Malformed databases and multithreading

Simon Slavin-3

On 14 Apr 2017, at 10:53pm, Paul Egli <[hidden email]> wrote:

> On the "how to corrupt" page ( http://sqlite.org/howtocorrupt.html ) i
> do not see any mention of using SQLite in an incorrect way with
> respect to thread safety. Is there really no way that, for example,
> using the same connection on multiple threads at the same time could
> either (a) corrupt the database, or (b) lead to a false reports of
> corruption?

Not unless you’re also violating one of the other things mentioned on that page.  SQLite implements mutex locking internally.  It is meant to prevent itself from messing up in the way you describe.

Please have their program execute

        PRAGMA compile_options;

and tell us any lines which include "THREAD" especially "SQLITE_THREADSAFE".  If this is set to zero you can have problems, because that copy of SQLite was compiled to omit thread safety code.  But this is not a default option and you should not be seeing it unless you did it on purpose.

You should probably read <https://sqlite.org/threadsafe.html>.

> […]
>
> They also routinely see "cannot start a transaction within a
> transaction" even though there is no obvious place where or reason why
> that would happen in their code. This leads us to believe they may be
> using the same connection on multiple threads.

I agree with your analysis, on what I know so far.  Have each program using SQLite execute

        sqlite3_config(SQLITE_CONFIG_MULTITHREAD)

/before it does any other SQLite call including sqlite3_initialize()/.  See whether that makes the problem go away.

Simon.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Malformed databases and multithreading

Jens Alfke-2
In reply to this post by Paul Egli

> On Apr 14, 2017, at 2:53 PM, Paul Egli <[hidden email]> wrote:
>
> On the "how to corrupt" page ( http://sqlite.org/howtocorrupt.html <http://sqlite.org/howtocorrupt.html> ) i
> do not see any mention of using SQLite in an incorrect way with
> respect to thread safety.

Huh, you’re right. That seems like an omission. This is covered elsewhere in the docs, but it should be on that page too.

> Is there really no way that, for example,
> using the same connection on multiple threads at the same time could
> either (a) corrupt the database, or (b) lead to a false reports of
> corruption?

Oh, it’s totally possible, if the connection doesn’t use the Serialized threading mode. (In fact it’s the most common cause of corruption that I’ve seen.)

—Jens
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Malformed databases and multithreading

Simon Slavin-3
In reply to this post by Simon Slavin-3

On 14 Apr 2017, at 11:37pm, Simon Slavin <[hidden email]> wrote:

> Have each program using SQLite execute
>
> sqlite3_config(SQLITE_CONFIG_MULTITHREAD)

That should have been

Have each program using SQLite execute

        sqlite3_config(SQLITE_CONFIG_MULTITHREAD)

Apologies.

Simon.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users