Skip to content

Commit f3c803b

Browse files
emontyjeffliu27
authored andcommitted
Update User.NumRepos atomically in createRepository (go-gitea#7493)
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.
1 parent 46089de commit f3c803b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

models/repo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,13 +1302,17 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
13021302
return err
13031303
}
13041304

1305-
u.NumRepos++
13061305
// Remember visibility preference.
13071306
u.LastRepoVisibility = repo.IsPrivate
1308-
if err = updateUser(e, u); err != nil {
1307+
if err = updateUserCols(e, u, "last_repo_visibility"); err != nil {
13091308
return fmt.Errorf("updateUser: %v", err)
13101309
}
13111310

1311+
if _, err = e.Incr("num_repos").ID(u.ID).Update(new(User)); err != nil {
1312+
return fmt.Errorf("increment user total_repos: %v", err)
1313+
}
1314+
u.NumRepos++
1315+
13121316
// Give access to all members in owner team.
13131317
if u.IsOrganization() {
13141318
t, err := u.getOwnerTeam(e)

0 commit comments

Comments
 (0)