Skip to content

Commit

Permalink
Update User.NumRepos atomically in createRepository (go-gitea#7493)
Browse files Browse the repository at this point in the history
The update call on the user call races if there is more than one
repository creation concurrently, leading to incorrect count of
repos. Split things in two, so that we call the update for last
visibility (which isn't problematic if it races, since it can only
ever be best-effort anyway). This way we can atomically increment
the count of repos.
  • Loading branch information
emonty authored and jeffliu27 committed Jul 18, 2019
1 parent 46089de commit f3c803b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,13 +1302,17 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
return err
}

u.NumRepos++
// Remember visibility preference.
u.LastRepoVisibility = repo.IsPrivate
if err = updateUser(e, u); err != nil {
if err = updateUserCols(e, u, "last_repo_visibility"); err != nil {
return fmt.Errorf("updateUser: %v", err)
}

if _, err = e.Incr("num_repos").ID(u.ID).Update(new(User)); err != nil {
return fmt.Errorf("increment user total_repos: %v", err)
}
u.NumRepos++

// Give access to all members in owner team.
if u.IsOrganization() {
t, err := u.getOwnerTeam(e)
Expand Down

0 comments on commit f3c803b

Please sign in to comment.