Skip to content

Commit

Permalink
fix: use SetMaxOpenConns to fix database is close issue
Browse files Browse the repository at this point in the history
fixes error "database is locked", caused by concurrent access from deal goroutines to a single sqlite3 db connection
see: https://github.com/mattn/go-sqlite3#:~:text=Error%3A%20database%20is%20locked
see: filecoin-project/boost#657

Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed May 16, 2023
1 parent 3194de2 commit e7278a9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions go/internal/accountutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,14 @@ func GetGormDBForPath(dbPath string, key []byte, salt []byte, logger *zap.Logger
return nil, nil, errcode.TODO.Wrap(err)
}

sqlDB, err := db.DB()
if err != nil {
return nil, nil, fmt.Errorf("unable to gorm underlying db: %w", err)
}

sqlDB.SetMaxOpenConns(1)

return db, func() {
sqlDB, _ := db.DB()
if sqlDB != nil {
sqlDB.Close()
}
_ = sqlDB.Close()
}, nil
}
2 changes: 1 addition & 1 deletion go/internal/messengerdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,7 @@ func (d *DBWrapper) MarkAccountDirectoryServiceRecordAsRevoked(serverAddr string
return d.db.Transaction(func(tx *gorm.DB) error {
record := &messengertypes.AccountDirectoryServiceRecord{}

query := d.db.Model(&messengertypes.AccountDirectoryServiceRecord{}).Where(record, &messengertypes.AccountDirectoryServiceRecord{
query := tx.Model(&messengertypes.AccountDirectoryServiceRecord{}).Where(record, &messengertypes.AccountDirectoryServiceRecord{
ServerAddr: serverAddr,
DirectoryRecordToken: token,
}, "expiration_date <= ?", removalDate).Update("revoked", true)
Expand Down
2 changes: 2 additions & 0 deletions go/internal/messengerdb/db_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func GetInMemoryTestDB(t testing.TB, opts ...GetInMemoryTestDBOpts) (*DBWrapper,
t.Fatal(err)
}

d.SetMaxOpenConns(1)

return wrappedDB, db, func() {
_ = d.Close()
loggerCleanup()
Expand Down

0 comments on commit e7278a9

Please sign in to comment.