Signal-Android/src/org/thoughtcrime/securesms/database
Kevin Mark 88f9ec313f Close SQL statement, preventing finalizer crashes
This will stop instances of the following from occuring in the logs
on SMS migration:

W/SQLiteCompiledSql: Releasing statement in a finalizer. Please ensure
that you explicitly call close() on your cursor: INSERT INTO sms
(address, person, date_sent, date, protocol, read, status, type,
reply_path_present,
    net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
        at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
        at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:109)
        at net.sqlcipher.database.SQLiteStatement.<init>(SQLiteStatement.java:39)
        at net.sqlcipher.database.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1647)
        at org.thoughtcrime.securesms.database.SmsDatabase.createInsertStatement(SmsDatabase.java:767)
        at org.thoughtcrime.securesms.database.SmsMigrator.migrateConversation(SmsMigrator.java:166)
        at org.thoughtcrime.securesms.database.SmsMigrator.migrateDatabase(SmsMigrator.java:210)
        at org.thoughtcrime.securesms.service.ApplicationMigrationService$ImportRunnable.run(ApplicationMigrationService.java:159)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

We aren't closing Statement objects before the finalizer on those
objects runs. When the GC runs, we'll get warnings like the above
which alert us to the fact that these objects are being automatically
closed for us in the finalizer, but that this is suboptimal behavior.

If we leave too many Statement (or Cursor) objects to be closed in
their finalizers, when the GC runs, it'll take longer than 10 seconds
to close them all and Android will kill the app. This 10 second limit
is hardcoded and we can only try to avoid it. A crash will look like:

java.util.concurrent.TimeoutException: net.sqlcipher.database.SQLiteCompiledSql.finalize() timed out after 10 seconds
    at java.lang.Object.wait(Native Method)
    at java.lang.Thread.parkFor$(Thread.java:1220)
    at sun.misc.Unsafe.park(Unsafe.java:299)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:844)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173)
    at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:196)
    at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:257)
    at net.sqlcipher.database.SQLiteDatabase.lock(SQLiteDatabase.java:553)
    at net.sqlcipher.database.SQLiteCompiledSql.releaseSqlStatement(SQLiteCompiledSql.java:106)
    at net.sqlcipher.database.SQLiteCompiledSql.finalize(SQLiteCompiledSql.java:152)
    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
    at java.lang.Thread.run(Thread.java:818)

I was able to replicate the above crash consistently on a
Samsung Galaxy S7 edge when importing well over 100k SMS messages.
But as soon as I attached a debugger the crash did not persist. I
assume this is because of some VM-level interactions between the two
and did not investigate further after fixing it.

I do not have access to the stack trace for issue #7953 but this
could potentially resolve it. The crash is identical to that in #7477
but this patch is for SMS migration not restoring from a backup. I
was not able to replicate the crash on restoring a >100k message
backup.
2019-04-30 12:51:57 -07:00
..
documents Switch logs to use new Log class. 2018-08-06 10:50:06 -04:00
helpers Migrated to new JobManager. 2019-04-15 10:56:26 -04:00
identity No need for a RecipientFactory any longer 2017-08-28 10:30:50 -07:00
loaders Remove unnecessary pre-19 code branches. 2019-03-21 11:19:06 -07:00
model Make DisplayRecord take context as argument, so locale is more likely to be correct. 2019-03-13 18:28:16 -03:00
Address.java Add processing rules for partial US and BR phone numbers. 2019-01-15 11:57:42 -08:00
ApnDatabase.java Reduce usage of Log.w() 2018-08-06 10:50:06 -04:00
AttachmentDatabase.java Upload attachments in a separate job. 2019-01-13 19:25:40 -08:00
ContentValuesBuilder.java Significant MMS changes 2017-05-08 18:14:55 -07:00
CursorList.java Implemented conversation search. 2019-02-14 20:19:01 -08:00
CursorRecyclerViewAdapter.java Implemented conversation search. 2019-02-14 20:19:01 -08:00
Database.java Create placeholder ContentProviders for database observations. 2018-09-08 07:48:20 -07:00
DatabaseContentProviders.java Create placeholder ContentProviders for database observations. 2018-09-08 07:48:20 -07:00
DatabaseFactory.java Migrated to new JobManager. 2019-04-15 10:56:26 -04:00
DraftDatabase.java Save replies in drafts. 2018-04-26 09:19:22 -07:00
EarlyReceiptCache.java Reduce usage of Log.w() 2018-08-06 10:50:06 -04:00
FastCursorRecyclerViewAdapter.java Switch logs to use new Log class. 2018-08-06 10:50:06 -04:00
GroupDatabase.java Simplify access to SecureRandom 2019-03-21 11:19:06 -07:00
GroupReceiptDatabase.java Support for sealed sender - Part 1 2018-10-30 08:48:08 -07:00
IdentityDatabase.java Migrate prekeys into database 2018-03-05 16:11:49 -08:00
JobDatabase.java Wrap transactions in try-finally. 2019-04-28 11:42:02 -07:00
MediaDatabase.java Added ability to receive long messages. 2019-03-01 14:15:08 -08:00
MessagingDatabase.java Support for sealed sender - Part 2 2018-10-30 08:48:08 -07:00
MmsDatabase.java Migrated to new JobManager. 2019-04-15 10:56:26 -04:00
MmsSmsColumns.java Prevent SendJobs from sending already-sent messages. 2018-11-14 16:20:55 -08:00
MmsSmsDatabase.java Implemented conversation search. 2019-02-14 20:19:01 -08:00
NoExternalStorageException.java Initial Project Import 2011-12-20 10:20:44 -08:00
NoSuchMessageException.java Transition the outbound pipeline to JobManager jobs. 2014-11-12 15:26:25 -08:00
NotInDirectoryException.java Add account management interface to libtextsecure api 2014-11-12 15:28:08 -08:00
OneTimePreKeyDatabase.java Switch logs to use new Log class. 2018-08-06 10:50:06 -04:00
PushDatabase.java Support for sealed sender - Part 1 2018-10-30 08:48:08 -07:00
RecipientDatabase.java Save the SMS setting on a per-conversation basis. 2019-04-15 10:13:35 -04:00
SearchDatabase.java Implemented conversation search. 2019-02-14 20:19:01 -08:00
SessionDatabase.java Switch logs to use new Log class. 2018-08-06 10:50:06 -04:00
SignedPreKeyDatabase.java Switch logs to use new Log class. 2018-08-06 10:50:06 -04:00
SmsDatabase.java Migrated to new JobManager. 2019-04-15 10:56:26 -04:00
SmsMigrator.java Close SQL statement, preventing finalizer crashes 2019-04-30 12:51:57 -07:00
ThreadDatabase.java Make DisplayRecord take context as argument, so locale is more likely to be correct. 2019-03-13 18:28:16 -03:00
XmlBackup.java Fix implicit locale string formatting bugs 2017-10-04 11:36:43 -07:00