Skip to content

Commit

Permalink
fix: Prevent crashes in multithreaded environments with sqlite.
Browse files Browse the repository at this point in the history
  • Loading branch information
crleona committed Apr 22, 2024
1 parent 8bf7c7e commit ec9da51
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/Amplitude/AMPDatabaseHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ @implementation AMPDatabaseHelper {

static NSString *const SEQUENCE_NUMBER = @"sequence_number";

static int const OPEN_DB_FLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;

+ (AMPDatabaseHelper *)getDatabaseHelper {
return [AMPDatabaseHelper getDatabaseHelper:nil];
}
Expand Down Expand Up @@ -159,7 +161,7 @@ - (BOOL)inDatabase:(void (^)(sqlite3 *db))block {
__block BOOL success = YES;

dispatch_sync(_queue, ^{
if (sqlite3_open([self->_databasePath UTF8String], &self->_database) != SQLITE_OK) {
if (sqlite3_open_v2(self->_databasePath.UTF8String, &self->_database, OPEN_DB_FLAGS, NULL) != SQLITE_OK) {
AMPLITUDE_LOG(@"Failed to open database");
sqlite3_close(self->_database);
success = NO;
Expand Down Expand Up @@ -195,7 +197,7 @@ - (BOOL)inDatabaseWithStatement:(NSString *)SQLString block:(void (^)(sqlite3_st
__block BOOL success = YES;

dispatch_sync(_queue, ^{
if (sqlite3_open([self->_databasePath UTF8String], &self->_database) != SQLITE_OK) {
if (sqlite3_open_v2(self->_databasePath.UTF8String, &self->_database, OPEN_DB_FLAGS, NULL) != SQLITE_OK) {
AMPLITUDE_LOG(@"Failed to open database");
sqlite3_close(self->_database);
success = NO;
Expand Down

0 comments on commit ec9da51

Please sign in to comment.