From 925fb118af9016f56488f8ae19258c1d2cb6bc20 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Mon, 23 Mar 2026 20:13:39 +0600 Subject: [PATCH] Potentially fix 'database is locked' crash # Conflicts: # CHANGELOG.md # app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt --- app/build.gradle.kts | 1 - .../java/eu/kanade/tachiyomi/di/AppModule.kt | 55 +++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7614b82b0..1e0c3a534 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -204,7 +204,6 @@ dependencies { implementation(sylibs.sqlcipher) // SY <-- - implementation(libs.kotlin.reflect) implementation(libs.kotlinx.collections.immutable) diff --git a/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt b/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt index 30ed8ae9b..bc962217f 100755 --- a/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt @@ -53,40 +53,49 @@ import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.InjektRegistrar import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy +import java.lang.ref.WeakReference + +private val lock = Any() class AppModule(val app: Application) : InjektModule { // SY --> private val securityPreferences: SecurityPreferences by injectLazy() // SY <-- + private var sqlDriverRef: WeakReference? = null + override fun InjektRegistrar.registerInjectables() { addSingleton(app) addSingletonFactory { - // SY --> - if (securityPreferences.encryptDatabase.get()) { - System.loadLibrary("sqlcipher") + synchronized(lock) { + sqlDriverRef?.get()?.let { return@synchronized it } - return@addSingletonFactory AndroidSqliteDriver( - schema = Database.Schema, - context = app, - name = CbzCrypto.DATABASE_NAME, - factory = SupportOpenHelperFactory(CbzCrypto.getDecryptedPasswordSql(), null, false, 25), - callback = object : AndroidSqliteDriver.Callback(Database.Schema) { - override fun onOpen(db: SupportSQLiteDatabase) { - super.onOpen(db) - setPragma(db, "foreign_keys = ON") - setPragma(db, "journal_mode = WAL") - setPragma(db, "synchronous = NORMAL") - } + // SY --> + if (securityPreferences.encryptDatabase.get()) { + System.loadLibrary("sqlcipher") - private fun setPragma(db: SupportSQLiteDatabase, pragma: String) { - val cursor = db.query("PRAGMA $pragma") - cursor.moveToFirst() - cursor.close() - } - }, - ) + return@synchronized AndroidSqliteDriver( + schema = Database.Schema, + context = app, + name = CbzCrypto.DATABASE_NAME, + factory = SupportOpenHelperFactory(CbzCrypto.getDecryptedPasswordSql(), null, false, 25), + callback = object : AndroidSqliteDriver.Callback(Database.Schema) { + override fun onOpen(db: SupportSQLiteDatabase) { + super.onOpen(db) + setPragma(db, "foreign_keys = ON") + setPragma(db, "journal_mode = WAL") + setPragma(db, "synchronous = NORMAL") + } + + private fun setPragma(db: SupportSQLiteDatabase, pragma: String) { + val cursor = db.query("PRAGMA $pragma") + cursor.moveToFirst() + cursor.close() + } + }, + ).also { sqlDriverRef = WeakReference(it) } + } } // SY <-- @@ -97,7 +106,7 @@ class AppModule(val app: Application) : InjektModule { configuration = AndroidxSqliteConfiguration( isForeignKeyConstraintsEnabled = true, ), - ) + ).also { sqlDriverRef = WeakReference(it) } } addSingletonFactory { Database(