sqlcipher / android-database-sqlcipher

Android SQLite API based on SQLCipher
https://www.zetetic.net/sqlcipher/sqlcipher-for-android/
Other
2.73k stars 564 forks source link

java.lang.IllegalStateException: database /data/user/0/<app>/databases/<app>_encrypted.db already closed #628

Closed zkrige closed 7 months ago

zkrige commented 11 months ago

Expected Behavior

DB stays open

Actual Behavior

DB is being closed at some point

at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:57)
at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:91)
at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:50)
at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2019)
at net.sqlcipher.database.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1802)
at net.sqlcipher.database.SQLiteDatabase.query(SQLiteDatabase.java:1754)
at net.sqlcipher.database.SQLiteDatabase.query(SQLiteDatabase.java:1886)

Steps to Reproduce

.... SQLCipher version (can be identified by executing PRAGMA cipher_version;): 4.54 community

SQLCipher for Android version: 4.54

Are you able to reproduce this issue within the SQLCipher for Android test suite? No

The db seems to behave for a while, and then at some point, it gets closed. I have multiple reads and writes happening from different background tasks.

This is an existing app that was using standard android sqlite. I've just done a drop in replacement for the sqlcipher version. If I revert to standard android sqlite, it works fine

sjlombardo commented 10 months ago

There is not enough information here to determine what the problem is. Are all of the threads you mentioned sharing a single database connection? Or does each thread have it's own separate connection? Have you determined if there are any exceptions being thrown that might be causing one of your application's threads to close the database connection prematurely?

zkrige commented 10 months ago

Im having a hard time creating a test app that reproduces this, but im definitely seeing quite a bit of it in our production app

zkrige commented 10 months ago

here's a screenshot from crashlytics showing a bunch of crashes in our prod app

image
sjlombardo commented 10 months ago

Hello @zkrige - I definitely believe the problem is occurring for you. However, screenshots of Crashlytics don't provide any useable information. Can you please answer the questions earlier in this thread? It is possible that it is a bug, but it is much more likely that somehow one of your threads is closing the database handle unexpectedly. I would really recommend checking any logic related to closing database connections.

zkrige commented 10 months ago

We don't have any calls to close db connections. The DB is provided via a singleton that gets instantiated early on in app lifecycle. It is never closed or released.

preliminary testing points to something in the query component going awry but I can't seem to pin down the exact scenario causing it. The reference count is at some point decremented once too many and the library then thinks the db is closed

zkrige commented 10 months ago

I'm not using this library anymore - have moved to https://github.com/sqlcipher/sqlcipher-android/ - will monitor and see if it continues

sjlombardo commented 7 months ago

Closing as logged against the wrong library, discussion moved to referenced ticket.