Signal-iOS/SignalMessaging
Michelle Linington bcb7eddb63 Fix issue with data migrations after a schema change
We're seeing crashes during this data migration step. We fail to init
the OWSUserProfile from the Row result returned from the SQLite fetch
request. It's likely we're indexing into an out-of-bounds index on Row.
(Row._checkIndex is inlined so we can't be sure)

GRDB caches the sqlite3_column_count during Row.init and uses that to
determine if a subscript access is out-of-bounds. From what I can tell
in an amateur reading of sqlite3.c, that just pulls the nResColumn from
the prepared statement. SQLite will determine the number of result
columns during statement prep, but the column count can change if the
table schema changes:

> If the database schema changes, instead of returning SQLITE_SCHEMA as
  it always used to do, sqlite3_step() will automatically recompile the
  SQL statement and try to run it again. As many as
  SQLITE_MAX_SCHEMA_RETRY retries will occur before sqlite3_step() gives
  up and returns an error.

We're nesting a deferred read transaction inside of our data migration's
immediate transaction. This read is using a different sqlite connection
that the connection performing our migrations. So its understanding of
the schema is stale and because it's in a deferred transaction it won't
figure this out until we call _step().

Once we call _step(), sqlite will realize that the table schema has
changed and recompile the prepared statement while opening the
deferred transaction. After this, the sqlite_column_count is correct.
But Row still has the incorrect value cached from before the schema was
resolved.

So by the time we try and init our OWSUserProfile, Row falsely asserts
that we're indexing beyond the column count by comparing against a stale
version of the column count.

The fix:
Rework the data migration to avoid nesting a deferred read transaction.
This is incorrect behavior. Now that our read is using our already open
write transaction with the same connection handle, it's understanding of
the schema is up-to-date during statement prep. So Row.init grabs the
current column count.
2021-09-30 13:00:26 -07:00
..
appearance Assert that GRDB schema/data migrations are complete before we warm caches. 2021-09-23 10:15:34 -03:00
attachments Convert to new Promise library 2021-09-03 11:41:34 -07:00
categories Add logging around websocket lifecycle. 2021-08-30 21:26:49 -03:00
contacts Simplify web socket background state. 2021-09-08 14:59:41 -03:00
environment Handle certain call messages sync. 2021-09-22 12:12:10 -07:00
groups Convert to new Promise library 2021-09-03 11:41:34 -07:00
Notifications Improve handling of voip pushes on iOS 15. 2021-09-30 16:41:32 -03:00
Payments Assert that GRDB schema/data migrations are complete before we warm caches. 2021-09-23 10:15:34 -03:00
profiles Fix issue with data migrations after a schema change 2021-09-30 13:00:26 -07:00
Storage Service Convert to new Promise library 2021-09-03 11:41:34 -07:00
utils Wait for RingRTC before we finish handling VoIP push 2021-09-24 18:36:35 -07:00
ViewControllers Fix table bottom view layout. 2021-09-09 18:26:19 -03:00
ViewModels Lint 2021-08-26 22:32:30 -07:00
Views Improve handling of voip pushes on iOS 15. 2021-09-30 16:41:32 -03:00
Wallpapers Assert that GRDB schema/data migrations are complete before we warm caches. 2021-09-23 10:15:34 -03:00
Info.plist Shared framework between app and extension 2017-11-29 13:58:27 -08:00
SignalMessaging-Prefix.pch Label the "slow write transactions" logging with the source of the transaction. 2020-06-07 10:37:55 -03:00
SignalMessaging.h Rework avatar building and caching. 2021-06-18 14:01:32 -03:00