Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FirebaseMessaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 12.8.0
- [fixed] Fix missing database crash on launch. (#14880)

# 12.1.0
- [fixed] Fix Xcode 26 crash from missing `NSUserActivityTypeBrowsingWeb`
symbol. Note that this fix isn't in the 12.1.0 zip and Carthage
Expand Down
50 changes: 37 additions & 13 deletions FirebaseMessaging/Sources/FIRMessagingRmqManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ - (void)removeDatabase {
});
}

- (void)createTable {
[self createTableWithName:kTableOutgoingRmqMessages command:kCreateTableOutgoingRmqMessages];
[self createTableWithName:kTableLastRmqId command:kCreateTableLastRmqId];
[self createTableWithName:kTableS2DRmqIds command:kCreateTableS2DRmqIds];
}

- (void)openDatabase {
dispatch_async(_databaseOperationQueue, ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
Expand All @@ -512,26 +518,44 @@ - (void)openDatabase {
NSAssert(NO, errorMessage);
return;
}
[self createTableWithName:kTableOutgoingRmqMessages command:kCreateTableOutgoingRmqMessages];

[self createTableWithName:kTableLastRmqId command:kCreateTableLastRmqId];
[self createTableWithName:kTableS2DRmqIds command:kCreateTableS2DRmqIds];
[self createTable];
} else {
// Calling sqlite3_open should create the database, since the file doesn't exist.
// The file exists, try to open it. If it fails, it might be corrupt.
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
#ifdef SQLITE_OPEN_FILEPROTECTION_NONE
flags |= SQLITE_OPEN_FILEPROTECTION_NONE;
#endif
int result = sqlite3_open_v2([path UTF8String], &self->_database, flags, NULL);

// If opening the database failed, it might be corrupt. Try to recover by deleting and
// recreating it.
if (result != SQLITE_OK) {
NSString *errorString = FIRMessagingStringFromSQLiteResult(result);
NSString *errorMessage =
[NSString stringWithFormat:@"Could not create RMQ database at path %@, error: %@", path,
errorString];
FIRMessagingLoggerError(kFIRMessagingMessageCodeRmq2PersistentStoreErrorCreatingDatabase,
@"%@", errorMessage);
NSAssert(NO, errorMessage);
didOpenDatabase = NO;
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
@"Could not open RMQ database at path: %@. "
"Will delete and try to recreate it.",
path);
NSError *removeError;
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&removeError]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know the exact error code SQLite returns when the database is corrupt? Deleting the database for all error codes seems a bit extreme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to #14880 , the error code is 14

Could not create RMQ database at path /var/mobile/Containers/Data/Application/372D4296-3F35-46FD-B3C9-0A20877B8378/Library/Application Support/Google/FirebaseMessaging/rmq2.sqlite, error: 14 - unable to open database file

How about updating the code to only delete the file if the result is SQLITE_CANTOPEN?
https://sqlite.org/rescode.html

(14) SQLITE_CANTOPEN
The SQLITE_CANTOPEN result code indicates that SQLite was unable to open a file. The file in question might be a primary database file or one of several temporary disk files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the quick merge. Addressing feedback in #15678

FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
@"Failed to delete corrupt database at %@: %@", path, removeError);
}
// After deleting, try to open it again.
result = sqlite3_open_v2([path UTF8String], &self->_database, flags, NULL);
// If it still fails after the recovery attempt, then assert and crash.
if (result != SQLITE_OK) {
NSString *errorString = FIRMessagingStringFromSQLiteResult(result);
NSString *errorMessage = [NSString
stringWithFormat:@"Could not open or create RMQ database at path %@, error: %@", path,
errorString];
FIRMessagingLoggerError(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
@"%@", errorMessage);
NSAssert(NO, errorMessage);
didOpenDatabase = NO; // Still failed, so indicate database did not open.
} else {
// Successfully recreated after corruption, so treat as a new database for table creation.
didOpenDatabase = YES; // Indicate successful opening after recreation.
[self createTable];
}
} else {
[self updateDBWithStringRmqID];
}
Expand Down
Loading