Skip to content

Commit 87074ec

Browse files
lunnyzeripath6543
authored
Fix delete nonexist oauth application 500 and prevent deadlock (#15384) (#15396)
* Fix delete nonexist oauth application 500 * Fix test * Close the session * Fix more missed sess.Close * Remove unnecessary blank line Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 1fe5fe4 commit 87074ec

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

integrations/api_oauth2_apps_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func testAPIDeleteOAuth2Application(t *testing.T) {
9292
session.MakeRequest(t, req, http.StatusNoContent)
9393

9494
models.AssertNotExistsBean(t, &models.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name})
95+
96+
// Delete again will return not found
97+
req = NewRequest(t, "DELETE", urlStr)
98+
session.MakeRequest(t, req, http.StatusNotFound)
9599
}
96100

97101
func testAPIGetOAuth2Application(t *testing.T) {

models/migrate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func InsertMilestones(ms ...*Milestone) (err error) {
3939
// InsertIssues insert issues to database
4040
func InsertIssues(issues ...*Issue) error {
4141
sess := x.NewSession()
42+
defer sess.Close()
4243
if err := sess.Begin(); err != nil {
4344
return err
4445
}
@@ -194,6 +195,7 @@ func InsertPullRequests(prs ...*PullRequest) error {
194195
// InsertReleases migrates release
195196
func InsertReleases(rels ...*Release) error {
196197
sess := x.NewSession()
198+
defer sess.Close()
197199
if err := sess.Begin(); err != nil {
198200
return err
199201
}

models/oauth2_application.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
235235
if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
236236
return err
237237
} else if deleted == 0 {
238-
return fmt.Errorf("cannot find oauth2 application")
238+
return ErrOAuthApplicationNotFound{ID: id}
239239
}
240240
codes := make([]*OAuth2AuthorizationCode, 0)
241241
// delete correlating auth codes
@@ -261,6 +261,7 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
261261
// DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app.
262262
func DeleteOAuth2Application(id, userid int64) error {
263263
sess := x.NewSession()
264+
defer sess.Close()
264265
if err := sess.Begin(); err != nil {
265266
return err
266267
}

routers/api/v1/user/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,11 @@ func DeleteOauth2Application(ctx *context.APIContext) {
274274
// "$ref": "#/responses/empty"
275275
appID := ctx.ParamsInt64(":id")
276276
if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
277-
ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
277+
if models.IsErrOAuthApplicationNotFound(err) {
278+
ctx.NotFound()
279+
} else {
280+
ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
281+
}
278282
return
279283
}
280284

0 commit comments

Comments
 (0)