Skip to content

Commit 4d2a6c4

Browse files
authored
[Backport] Fix Deadlock & Delete affected reactions on comment deletion (#14392) (#14425)
* Enhance Ghost comment mitigation Settings (#14392) * refactor models.DeleteComment and delete related reactions too * use deleteComment for UserDeleteWithCommentsMaxDays in DeleteUser * Resolve Fixme & fix potential deadlock * rm refactor * make diff eaven less
1 parent fb274ec commit 4d2a6c4

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

models/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func removeStorageWithNotice(e Engine, bucket storage.ObjectStorage, title, path
7777
if err := bucket.Delete(path); err != nil {
7878
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
7979
log.Warn(title+" [%s]: %v", path, err)
80-
if err = createNotice(x, NoticeRepository, desc); err != nil {
80+
if err = createNotice(e, NoticeRepository, desc); err != nil {
8181
log.Error("CreateRepositoryNotice: %v", err)
8282
}
8383
}

models/issue_assignees.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func isUserAssignedToIssue(e Engine, issue *Issue, user *User) (isAssigned bool,
8282
}
8383

8484
// ClearAssigneeByUserID deletes all assignments of an user
85-
func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
85+
func clearAssigneeByUserID(sess Engine, userID int64) (err error) {
8686
_, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
8787
return
8888
}

models/issue_comment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,10 @@ func DeleteComment(comment *Comment, doer *User) error {
10771077
return err
10781078
}
10791079

1080+
if err := deleteReaction(sess, &ReactionOptions{Comment: comment}); err != nil {
1081+
return err
1082+
}
1083+
10801084
return sess.Commit()
10811085
}
10821086

models/issue_reaction.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ func CreateCommentReaction(doer *User, issue *Issue, comment *Comment, content s
178178
})
179179
}
180180

181-
func deleteReaction(e *xorm.Session, opts *ReactionOptions) error {
181+
func deleteReaction(e Engine, opts *ReactionOptions) error {
182182
reaction := &Reaction{
183-
Type: opts.Type,
184-
UserID: opts.Doer.ID,
185-
IssueID: opts.Issue.ID,
183+
Type: opts.Type,
184+
}
185+
if opts.Doer != nil {
186+
reaction.UserID = opts.Doer.ID
187+
}
188+
if opts.Issue != nil {
189+
reaction.IssueID = opts.Issue.ID
186190
}
187191
if opts.Comment != nil {
188192
reaction.CommentID = opts.Comment.ID

models/user.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"golang.org/x/crypto/scrypt"
4141
"golang.org/x/crypto/ssh"
4242
"xorm.io/builder"
43-
"xorm.io/xorm"
4443
)
4544

4645
// UserType defines the user type
@@ -1020,8 +1019,7 @@ func deleteBeans(e Engine, beans ...interface{}) (err error) {
10201019
return nil
10211020
}
10221021

1023-
// FIXME: need some kind of mechanism to record failure. HINT: system notice
1024-
func deleteUser(e *xorm.Session, u *User) error {
1022+
func deleteUser(e Engine, u *User) error {
10251023
// Note: A user owns any repository or belongs to any organization
10261024
// cannot perform delete operation.
10271025

@@ -1135,18 +1133,21 @@ func deleteUser(e *xorm.Session, u *User) error {
11351133
return fmt.Errorf("Delete: %v", err)
11361134
}
11371135

1138-
// FIXME: system notice
11391136
// Note: There are something just cannot be roll back,
11401137
// so just keep error logs of those operations.
11411138
path := UserPath(u.Name)
1142-
if err := util.RemoveAll(path); err != nil {
1143-
return fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
1139+
if err = util.RemoveAll(path); err != nil {
1140+
err = fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
1141+
_ = createNotice(e, NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
1142+
return err
11441143
}
11451144

11461145
if len(u.Avatar) > 0 {
11471146
avatarPath := u.CustomAvatarRelativePath()
1148-
if err := storage.Avatars.Delete(avatarPath); err != nil {
1149-
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
1147+
if err = storage.Avatars.Delete(avatarPath); err != nil {
1148+
err = fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
1149+
_ = createNotice(e, NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
1150+
return err
11501151
}
11511152
}
11521153

0 commit comments

Comments
 (0)