Repository navigation
Improve OfflineDatabase reliability #10031
Description
There are a number of issues with our current handling of errors in OfflineDatabase:
- Violations of iOS data protection rules cause error when doing any operations with the database. Since SQLite throws an error in this case, which isn't caught by our code, we crash.
- When a database file becomes corrupt, SQLite throws an error when opening, migrating, or doing any other operation with the database. We're currently not catching this error and crash instead.
- When the disk becomes full, SQLite errors as well, when inserting or vacuuming and we crash (see Uncaught exception when database or disk is full #7164)
iOS data protection
Data protection is only an issue when the developer enables it in the Entitlements section of the app, which isn't something that we have control over. Violations can occur when we are trying to access the file system when the device is locked. We attempted to fix this problem in #6205, however, the pause() function is enqueued as a regular method into that object's mailbox. When enough actions are enqueued before it, it's possible that data protection kicks in when the user locks the device while we're still processing the queue.
Apple's copy of SQLite has the ability to set SQLITE_OPEN_FILEPROTECTION_NONE when opening the database file. However, it's only possible to open this file when it already exists, and has the NSFileProtectionNone flag already set. We can't guarantee this since opening the database happens on a separate thread and is therefore prone to timing issues as described above. Similarly, even when a file has the NSFileProtectionNone flag already set, and the developer changes the entitlement for this app, all flags get reset. When enabling, all flags get set to NSFileProtectionComplete, and when disabling the entitlement, all flags get reset to NSFileProtectionCompleteUntilFirstUserAuthentication.
There are two next actions here:
- Change our
pause()/resume()calls so that they take effect much quicker, rather than adding this operation to the end of the queue. Shortening the time between thepause()call, and the processing stop going into effect will drastically reduce those situations where we continue to work with the database despite being in the background. iOS has a grace period of a few seconds before data protection kicks in after the device was locked. - Change the design of
OfflineDatabaseto allow for the absence of a database object altogether, and catch errors related to file protection:SQLITE_CANTOPENis thrown when the database doesn't exist and we attempt to create itSQLITE_AUTHis thrown when the database does exist and we attempt to open itSQLITE_IOERRis thrown when we've already opened the database, and continue to access it when the device is locked
We should change OfflineDatabase so that it will simply discard putTile/putResource operations when the database is inaccessible. Similarly, it should return "not found" errors when querying information from the database. Our code that deals with offline regions already catches SQLite exceptions and forwards them to the client implementation rather than crashing the application.
I also believe that we shouldn't disable file protection for our cache/offline database. While the data stored in this database is public information, the particular tile coordinates/resources stored in the database are sensitive information.
Database corruption
Database corruption occurs extremely rarely with SQLite when used as intended. By far the biggest reason for database corruption occur when application contains more than one copies of the SQLite library. On iOS, we're compiling against the system-provided version of SQLite. Some third party libraries like FMDB or SQLCipher may contain their own copy of SQLite. This can lead to database corruption and is not recommended.
That said, instead of crashing the application, we should deal with this error by removing the existing database, and recreating it from scratch. This is a very invasive operation since it'll delete the user's offline regions, so when you encounter database corruption messages, you should verify that you're not misusing SQLite in your application.
Database corruption errors can either occur directly when opening the database, or when running a SQL statement that accesses the corrupt part of the database. Therefore, we need to wrap all SQL statements into a try/catch as well so that we can handle the error in this case.
Disk full
This error can occur when the device doesn't have more disk space and we attempt to migrate, vacuum, or insert into the database. There's nothing per se wrong with the database, but we can't add more data to it. In this case, we should discard the current insertion operations rather than crashing the application.
/cc @mapbox/gl-core
Activity
kkaefer commentedon Aug 15, 2018
Fixed by #12224