Skip to content

Commit 6868378

Browse files
zeripathtechknowlogick
authored andcommitted
Ensure that sessions are passed into queries that could use the database to prevent deadlocks (#5718)
* Fixed deadlock in CreateComment * Fix possible deadlock in UpdateIssueDeadline from createDeadlineComment * Ensure that calls to IsTimeTracker enabled are called within session Signed-off-by: Andrew Thornton <[email protected]> * Ensure that calls to reactionList are also called within session Signed-off-by: Andrew Thornton <[email protected]> * Ensure all calls in NewPullRequest with the session are called within the session Signed-off-by: Andrew Thornton <[email protected]> * Deal with potential deadlocks in repo Signed-off-by: Andrew Thornton <[email protected]> * Ensure that isStaring is checked within our transaction Signed-off-by: Andrew Thornton <[email protected]> * Fix mistake in isOrganizationMember Sorry.
1 parent 6564564 commit 6868378

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

models/issue.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ func (issue *Issue) loadRepo(e Engine) (err error) {
103103

104104
// IsTimetrackerEnabled returns true if the repo enables timetracking
105105
func (issue *Issue) IsTimetrackerEnabled() bool {
106-
if err := issue.loadRepo(x); err != nil {
106+
return issue.isTimetrackerEnabled(x)
107+
}
108+
109+
func (issue *Issue) isTimetrackerEnabled(e Engine) bool {
110+
if err := issue.loadRepo(e); err != nil {
107111
log.Error(4, fmt.Sprintf("loadRepo: %v", err))
108112
return false
109113
}
@@ -196,7 +200,7 @@ func (issue *Issue) loadReactions(e Engine) (err error) {
196200
return err
197201
}
198202
// Load reaction user data
199-
if _, err := ReactionList(reactions).LoadUsers(); err != nil {
203+
if _, err := ReactionList(reactions).loadUsers(e); err != nil {
200204
return err
201205
}
202206

@@ -255,7 +259,7 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
255259
if err = issue.loadComments(e); err != nil {
256260
return err
257261
}
258-
if issue.IsTimetrackerEnabled() {
262+
if issue.isTimetrackerEnabled(e) {
259263
if err = issue.loadTotalTimes(e); err != nil {
260264
return err
261265
}

models/issue_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
559559
case CommentTypeCode:
560560
if comment.ReviewID != 0 {
561561
if comment.Review == nil {
562-
if err := comment.LoadReview(); err != nil {
562+
if err := comment.loadReview(e); err != nil {
563563
return err
564564
}
565565
}
@@ -709,7 +709,7 @@ func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlin
709709
content = newDeadlineUnix.Format("2006-01-02") + "|" + issue.DeadlineUnix.Format("2006-01-02")
710710
}
711711

712-
if err := issue.LoadRepo(); err != nil {
712+
if err := issue.loadRepo(e); err != nil {
713713
return nil, err
714714
}
715715

models/org.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ func IsOrganizationOwner(orgID, uid int64) (bool, error) {
314314

315315
// IsOrganizationMember returns true if given user is member of organization.
316316
func IsOrganizationMember(orgID, uid int64) (bool, error) {
317-
return x.
317+
return isOrganizationMember(x, orgID, uid)
318+
}
319+
320+
func isOrganizationMember(e Engine, orgID, uid int64) (bool, error) {
321+
return e.
318322
Where("uid=?", uid).
319323
And("org_id=?", orgID).
320324
Table("org_user").

models/pull.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,15 +721,15 @@ var patchConflicts = []string{
721721
}
722722

723723
// testPatch checks if patch can be merged to base repository without conflict.
724-
func (pr *PullRequest) testPatch() (err error) {
724+
func (pr *PullRequest) testPatch(e Engine) (err error) {
725725
if pr.BaseRepo == nil {
726-
pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
726+
pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
727727
if err != nil {
728728
return fmt.Errorf("GetRepositoryByID: %v", err)
729729
}
730730
}
731731

732-
patchPath, err := pr.BaseRepo.PatchPath(pr.Index)
732+
patchPath, err := pr.BaseRepo.patchPath(e, pr.Index)
733733
if err != nil {
734734
return fmt.Errorf("BaseRepo.PatchPath: %v", err)
735735
}
@@ -758,7 +758,7 @@ func (pr *PullRequest) testPatch() (err error) {
758758
return fmt.Errorf("git read-tree --index-output=%s %s: %v - %s", indexTmpPath, pr.BaseBranch, err, stderr)
759759
}
760760

761-
prUnit, err := pr.BaseRepo.GetUnit(UnitTypePullRequests)
761+
prUnit, err := pr.BaseRepo.getUnit(e, UnitTypePullRequests)
762762
if err != nil {
763763
return err
764764
}
@@ -811,12 +811,12 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
811811
}
812812

813813
pr.Index = pull.Index
814-
if err = repo.SavePatch(pr.Index, patch); err != nil {
814+
if err = repo.savePatch(sess, pr.Index, patch); err != nil {
815815
return fmt.Errorf("SavePatch: %v", err)
816816
}
817817

818818
pr.BaseRepo = repo
819-
if err = pr.testPatch(); err != nil {
819+
if err = pr.testPatch(sess); err != nil {
820820
return fmt.Errorf("testPatch: %v", err)
821821
}
822822
// No conflict appears after test means mergeable.
@@ -1363,7 +1363,7 @@ func TestPullRequests() {
13631363
if pr.manuallyMerged() {
13641364
continue
13651365
}
1366-
if err := pr.testPatch(); err != nil {
1366+
if err := pr.testPatch(x); err != nil {
13671367
log.Error(3, "testPatch: %v", err)
13681368
continue
13691369
}
@@ -1387,7 +1387,7 @@ func TestPullRequests() {
13871387
continue
13881388
} else if pr.manuallyMerged() {
13891389
continue
1390-
} else if err = pr.testPatch(); err != nil {
1390+
} else if err = pr.testPatch(x); err != nil {
13911391
log.Error(4, "testPatch[%d]: %v", pr.ID, err)
13921392
continue
13931393
}

models/repo.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,11 @@ func (repo *Repository) UpdateLocalCopyBranch(branch string) error {
776776

777777
// PatchPath returns corresponding patch file path of repository by given issue ID.
778778
func (repo *Repository) PatchPath(index int64) (string, error) {
779-
if err := repo.GetOwner(); err != nil {
779+
return repo.patchPath(x, index)
780+
}
781+
782+
func (repo *Repository) patchPath(e Engine, index int64) (string, error) {
783+
if err := repo.getOwner(e); err != nil {
780784
return "", err
781785
}
782786

@@ -785,7 +789,11 @@ func (repo *Repository) PatchPath(index int64) (string, error) {
785789

786790
// SavePatch saves patch data to corresponding location by given issue ID.
787791
func (repo *Repository) SavePatch(index int64, patch []byte) error {
788-
patchPath, err := repo.PatchPath(index)
792+
return repo.savePatch(x, index, patch)
793+
}
794+
795+
func (repo *Repository) savePatch(e Engine, index int64, patch []byte) error {
796+
patchPath, err := repo.patchPath(e, index)
789797
if err != nil {
790798
return fmt.Errorf("PatchPath: %v", err)
791799
}
@@ -1479,7 +1487,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
14791487
collaboration := &Collaboration{RepoID: repo.ID}
14801488
for _, c := range collaborators {
14811489
if c.ID != newOwner.ID {
1482-
isMember, err := newOwner.IsOrgMember(c.ID)
1490+
isMember, err := isOrganizationMember(sess, newOwner.ID, c.ID)
14831491
if err != nil {
14841492
return fmt.Errorf("IsOrgMember: %v", err)
14851493
} else if !isMember {
@@ -1536,12 +1544,12 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
15361544
if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
15371545
return fmt.Errorf("rename repository directory: %v", err)
15381546
}
1539-
RemoveAllWithNotice("Delete repository local copy", repo.LocalCopyPath())
1547+
removeAllWithNotice(sess, "Delete repository local copy", repo.LocalCopyPath())
15401548

15411549
// Rename remote wiki repository to new path and delete local copy.
15421550
wikiPath := UncycloPath(owner.Name, repo.Name)
15431551
if com.IsExist(wikiPath) {
1544-
RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalUncycloPath())
1552+
removeAllWithNotice(sess, "Delete repository wiki local copy", repo.LocalUncycloPath())
15451553
if err = os.Rename(wikiPath, UncycloPath(newOwner.Name, repo.Name)); err != nil {
15461554
return fmt.Errorf("rename repository wiki: %v", err)
15471555
}

models/star.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func StarRepo(userID, repoID int64, star bool) error {
2121
}
2222

2323
if star {
24-
if IsStaring(userID, repoID) {
24+
if isStaring(sess, userID, repoID) {
2525
return nil
2626
}
2727

@@ -35,7 +35,7 @@ func StarRepo(userID, repoID int64, star bool) error {
3535
return err
3636
}
3737
} else {
38-
if !IsStaring(userID, repoID) {
38+
if !isStaring(sess, userID, repoID) {
3939
return nil
4040
}
4141

@@ -55,7 +55,11 @@ func StarRepo(userID, repoID int64, star bool) error {
5555

5656
// IsStaring checks if user has starred given repository.
5757
func IsStaring(userID, repoID int64) bool {
58-
has, _ := x.Get(&Star{0, userID, repoID})
58+
return isStaring(x, userID, repoID)
59+
}
60+
61+
func isStaring(e Engine, userID, repoID int64) bool {
62+
has, _ := e.Get(&Star{0, userID, repoID})
5963
return has
6064
}
6165

0 commit comments

Comments
 (0)