Fix possible threading issues with attachment cleanup.

The way things were ordered, it was possible for us to create an
attachment file, but have it 'cleaned up' before we were able to link it
to an attachment row.
master
Greyson Parrelli 2020-09-24 16:51:15 -04:00
parent 864a1d5e93
commit e3ce18fa3e
2 changed files with 80 additions and 73 deletions

View File

@ -437,7 +437,7 @@ public class AttachmentDatabase extends Database {
public void trimAllAbandonedAttachments() {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String selectAllMmsIds = "SELECT " + MmsDatabase.ID + " FROM " + MmsDatabase.TABLE_NAME;
String selectDataInUse = "SELECT DISTINCT " + DATA + " FROM " + TABLE_NAME + " WHERE " + QUOTE + " = 0 AND " + MMS_ID + " IN (" + selectAllMmsIds + ")";
String selectDataInUse = "SELECT DISTINCT " + DATA + " FROM " + TABLE_NAME + " WHERE " + QUOTE + " = 0 AND (" + MMS_ID + " IN (" + selectAllMmsIds + ") OR " + MMS_ID + " = " + PREUPLOAD_MESSAGE_ID + ")";
String where = MMS_ID + " NOT IN (" + selectAllMmsIds + ") AND " + DATA + " NOT IN (" + selectDataInUse + ")";
db.delete(TABLE_NAME, where, null);
@ -1209,8 +1209,10 @@ public class AttachmentDatabase extends Database {
throws MmsException
{
Log.d(TAG, "Inserting attachment for mms id: " + mmsId);
SQLiteDatabase database = databaseHelper.getWritableDatabase();
database.beginTransaction();
try {
DataInfo dataInfo = null;
long uniqueId = System.currentTimeMillis();
@ -1289,7 +1291,12 @@ public class AttachmentDatabase extends Database {
notifyStickerPackListeners();
}
database.setTransactionSuccessful();
return attachmentId;
} finally {
database.endTransaction();
}
}
private @Nullable DatabaseAttachment findTemplateAttachment(@NonNull String dataHash) {

View File

@ -283,12 +283,12 @@ public class ThreadDatabase extends Database {
mmsSmsDatabase.deleteAbandonedMessages();
attachmentDatabase.trimAllAbandonedAttachments();
groupReceiptDatabase.deleteAbandonedRows();
attachmentDatabase.deleteAbandonedAttachmentFiles();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
attachmentDatabase.deleteAbandonedAttachmentFiles();
notifyAttachmentListeners();
notifyStickerListeners();
@ -312,12 +312,12 @@ public class ThreadDatabase extends Database {
mmsSmsDatabase.deleteAbandonedMessages();
attachmentDatabase.trimAllAbandonedAttachments();
groupReceiptDatabase.deleteAbandonedRows();
attachmentDatabase.deleteAbandonedAttachmentFiles();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
attachmentDatabase.deleteAbandonedAttachmentFiles();
notifyAttachmentListeners();
notifyStickerListeners();