Make FTS recovery more resiliant.

This commit is contained in:
Greyson Parrelli 2024-09-12 15:40:33 -04:00 committed by Cody Henthorne
parent 5c6644d1a1
commit 850515b363
2 changed files with 31 additions and 11 deletions

View file

@ -260,15 +260,24 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
try {
Log.w(TAG, "[fullyResetTables] Dropping tables and triggers...")
// Due to issues we've had in the past, the delete sequence here is very particular. It mimics the "safe drop" process in the SQLite source code
// that prevents weird vtable constructor issues when dropping potentially-corrupt tables. https://sqlite.org/src/info/4db9258a78?ln=1549-1592
if (SqlUtil.tableExists(db, FTS_TABLE_NAME)) {
db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_data")
db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_config")
db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_data VALUES(10, X'0000000000')")
db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_config VALUES('version', 4)")
db.execSQL("DROP TABLE $FTS_TABLE_NAME")
try {
db.execSQL("DROP TABLE IF EXISTS $FTS_TABLE_NAME")
} catch (e: SQLiteException) {
Log.w(TAG, "Failed to drop $FTS_TABLE_NAME! Attempting to do so a safer way.", e)
// Due to issues we've had in the past, the delete sequence here is very particular. It mimics the "safe drop" process in the SQLite source code
// that prevents weird vtable constructor issues when dropping potentially-corrupt tables. https://sqlite.org/src/info/4db9258a78?ln=1549-1592
db.withinTransaction {
if (SqlUtil.tableExists(db, FTS_TABLE_NAME)) {
db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_data")
db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_config")
db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_data VALUES(10, X'0000000000')")
db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_config VALUES('version', 4)")
db.execSQL("DROP TABLE $FTS_TABLE_NAME")
}
}
}
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_INSERT")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_DELETE")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_UPDATE")
@ -283,11 +292,11 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
Log.w(TAG, "[fullyResetTables] Done. Index will be rebuilt asynchronously)")
if (useTransaction) {
if (useTransaction && db.inTransaction()) {
db.setTransactionSuccessful()
}
} finally {
if (useTransaction) {
if (useTransaction && db.inTransaction()) {
db.endTransaction()
}
}

View file

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.util;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.database.sqlite.SQLiteException;
import androidx.annotation.NonNull;
@ -40,7 +41,7 @@ public class SignalUncaughtExceptionHandler implements Thread.UncaughtExceptionH
}
if (e instanceof SQLiteDatabaseCorruptException) {
if (e.getMessage().indexOf("message_fts") >= 0) {
if (e.getMessage() != null && e.getMessage().contains("message_fts")) {
Log.w(TAG, "FTS corrupted! Resetting FTS index.");
SignalDatabase.messageSearch().fullyResetTables();
} else {
@ -48,6 +49,16 @@ public class SignalUncaughtExceptionHandler implements Thread.UncaughtExceptionH
}
}
if (e instanceof SQLiteException && e.getMessage() != null) {
if (e.getMessage().contains("invalid fts5 file format")) {
Log.w(TAG, "FTS in invalid state! Resetting FTS index.");
SignalDatabase.messageSearch().fullyResetTables();
} else if (e.getMessage().contains("no such table: message_fts")) {
Log.w(TAG, "FTS table not found! Resetting FTS index.");
SignalDatabase.messageSearch().fullyResetTables();
}
}
if (e instanceof OnErrorNotImplementedException && e.getCause() != null) {
e = e.getCause();
}