Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent crashes in multithreaded environments with sqlite. #495

Merged
merged 1 commit into from
Apr 24, 2024
Merged
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
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
Loading