Protect User API SQLite calls with mutexes (#1201)

* Protect SQLite calls with mutexes (replaces #1200)

* Remove locks on reads
This commit is contained in:
Neil Alexander 2020-07-15 15:14:59 +01:00 committed by GitHub
parent 9dd2ed7f65
commit 84387e5249
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,7 +40,10 @@ type Database struct {
threepids threepidStatements threepids threepidStatements
serverName gomatrixserverlib.ServerName serverName gomatrixserverlib.ServerName
createAccountMu sync.Mutex accountsMu sync.Mutex
profilesMu sync.Mutex
accountDatasMu sync.Mutex
threepidsMu sync.Mutex
} }
// NewDatabase creates a new accounts and profiles database // NewDatabase creates a new accounts and profiles database
@ -74,7 +77,15 @@ func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName)
if err = t.prepare(db); err != nil { if err = t.prepare(db); err != nil {
return nil, err return nil, err
} }
return &Database{db, partitions, a, p, ac, t, serverName, sync.Mutex{}}, nil return &Database{
db: db,
PartitionOffsetStatements: partitions,
accounts: a,
profiles: p,
accountDatas: ac,
threepids: t,
serverName: serverName,
}, nil
} }
// GetAccountByPassword returns the account associated with the given localpart and password. // GetAccountByPassword returns the account associated with the given localpart and password.
@ -105,6 +116,8 @@ func (d *Database) GetProfileByLocalpart(
func (d *Database) SetAvatarURL( func (d *Database) SetAvatarURL(
ctx context.Context, localpart string, avatarURL string, ctx context.Context, localpart string, avatarURL string,
) error { ) error {
d.profilesMu.Lock()
defer d.profilesMu.Unlock()
return d.profiles.setAvatarURL(ctx, localpart, avatarURL) return d.profiles.setAvatarURL(ctx, localpart, avatarURL)
} }
@ -113,6 +126,8 @@ func (d *Database) SetAvatarURL(
func (d *Database) SetDisplayName( func (d *Database) SetDisplayName(
ctx context.Context, localpart string, displayName string, ctx context.Context, localpart string, displayName string,
) error { ) error {
d.profilesMu.Lock()
defer d.profilesMu.Unlock()
return d.profiles.setDisplayName(ctx, localpart, displayName) return d.profiles.setDisplayName(ctx, localpart, displayName)
} }
@ -124,8 +139,12 @@ func (d *Database) CreateGuestAccount(ctx context.Context) (acc *api.Account, er
// when the first txn upgrades to a write txn. We also need to lock the account creation else we can // when the first txn upgrades to a write txn. We also need to lock the account creation else we can
// race with CreateAccount // race with CreateAccount
// We know we'll be the only process since this is sqlite ;) so a lock here will be all that is needed. // We know we'll be the only process since this is sqlite ;) so a lock here will be all that is needed.
d.createAccountMu.Lock() d.profilesMu.Lock()
defer d.createAccountMu.Unlock() d.accountDatasMu.Lock()
d.accountsMu.Lock()
defer d.profilesMu.Unlock()
defer d.accountDatasMu.Unlock()
defer d.accountsMu.Unlock()
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
var numLocalpart int64 var numLocalpart int64
numLocalpart, err = d.accounts.selectNewNumericLocalpart(ctx, txn) numLocalpart, err = d.accounts.selectNewNumericLocalpart(ctx, txn)
@ -146,8 +165,12 @@ func (d *Database) CreateAccount(
ctx context.Context, localpart, plaintextPassword, appserviceID string, ctx context.Context, localpart, plaintextPassword, appserviceID string,
) (acc *api.Account, err error) { ) (acc *api.Account, err error) {
// Create one account at a time else we can get 'database is locked'. // Create one account at a time else we can get 'database is locked'.
d.createAccountMu.Lock() d.profilesMu.Lock()
defer d.createAccountMu.Unlock() d.accountDatasMu.Lock()
d.accountsMu.Lock()
defer d.profilesMu.Unlock()
defer d.accountDatasMu.Unlock()
defer d.accountsMu.Unlock()
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
acc, err = d.createAccount(ctx, txn, localpart, plaintextPassword, appserviceID) acc, err = d.createAccount(ctx, txn, localpart, plaintextPassword, appserviceID)
return err return err
@ -155,6 +178,8 @@ func (d *Database) CreateAccount(
return return
} }
// WARNING! This function assumes that the relevant mutexes have already
// been taken out by the caller (e.g. CreateAccount or CreateGuestAccount).
func (d *Database) createAccount( func (d *Database) createAccount(
ctx context.Context, txn *sql.Tx, localpart, plaintextPassword, appserviceID string, ctx context.Context, txn *sql.Tx, localpart, plaintextPassword, appserviceID string,
) (*api.Account, error) { ) (*api.Account, error) {
@ -196,6 +221,8 @@ func (d *Database) createAccount(
func (d *Database) SaveAccountData( func (d *Database) SaveAccountData(
ctx context.Context, localpart, roomID, dataType string, content json.RawMessage, ctx context.Context, localpart, roomID, dataType string, content json.RawMessage,
) error { ) error {
d.accountDatasMu.Lock()
defer d.accountDatasMu.Unlock()
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content) return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content)
}) })
@ -247,6 +274,8 @@ var Err3PIDInUse = errors.New("This third-party identifier is already in use")
func (d *Database) SaveThreePIDAssociation( func (d *Database) SaveThreePIDAssociation(
ctx context.Context, threepid, localpart, medium string, ctx context.Context, threepid, localpart, medium string,
) (err error) { ) (err error) {
d.threepidsMu.Lock()
defer d.threepidsMu.Unlock()
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
user, err := d.threepids.selectLocalpartForThreePID( user, err := d.threepids.selectLocalpartForThreePID(
ctx, txn, threepid, medium, ctx, txn, threepid, medium,
@ -270,6 +299,8 @@ func (d *Database) SaveThreePIDAssociation(
func (d *Database) RemoveThreePIDAssociation( func (d *Database) RemoveThreePIDAssociation(
ctx context.Context, threepid string, medium string, ctx context.Context, threepid string, medium string,
) (err error) { ) (err error) {
d.threepidsMu.Lock()
defer d.threepidsMu.Unlock()
return d.threepids.deleteThreePID(ctx, threepid, medium) return d.threepids.deleteThreePID(ctx, threepid, medium)
} }