Skip to content

Commit 361607d

Browse files
emontytechknowlogick
authored andcommitted
Update User.NumRepos atomically in createRepository (#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 0e2996c commit 361607d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

models/repo.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1306,13 +1306,17 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
13061306
return err
13071307
}
13081308

1309-
u.NumRepos++
13101309
// Remember visibility preference.
13111310
u.LastRepoVisibility = repo.IsPrivate
1312-
if err = updateUser(e, u); err != nil {
1311+
if err = updateUserCols(e, u, "last_repo_visibility"); err != nil {
13131312
return fmt.Errorf("updateUser: %v", err)
13141313
}
13151314

1315+
if _, err = e.Incr("num_repos").ID(u.ID).Update(new(User)); err != nil {
1316+
return fmt.Errorf("increment user total_repos: %v", err)
1317+
}
1318+
u.NumRepos++
1319+
13161320
// Give access to all members in owner team.
13171321
if u.IsOrganization() {
13181322
t, err := u.getOwnerTeam(e)

0 commit comments

Comments
 (0)