Skip to content

Commit 015d11d

Browse files
lunnyzeripath6543
authored
Fix delete nonexist oauth application 500 and prevent deadlock (#15384) (#15397)
* 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 62fbca3 commit 015d11d

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

integrations/api_oauth2_apps_test.go

+4
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

+2
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
233233
if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
234234
return err
235235
} else if deleted == 0 {
236-
return fmt.Errorf("cannot find oauth2 application")
236+
return ErrOAuthApplicationNotFound{ID: id}
237237
}
238238
codes := make([]*OAuth2AuthorizationCode, 0)
239239
// delete correlating auth codes
@@ -259,6 +259,7 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
259259
// 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.
260260
func DeleteOAuth2Application(id, userid int64) error {
261261
sess := x.NewSession()
262+
defer sess.Close()
262263
if err := sess.Begin(); err != nil {
263264
return err
264265
}

routers/api/v1/user/app.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ func DeleteOauth2Application(ctx *context.APIContext) {
268268
// "$ref": "#/responses/empty"
269269
appID := ctx.ParamsInt64(":id")
270270
if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
271-
ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
271+
if models.IsErrOAuthApplicationNotFound(err) {
272+
ctx.NotFound()
273+
} else {
274+
ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
275+
}
272276
return
273277
}
274278

0 commit comments

Comments
 (0)