From f3956fcb28e665ee7f2773f4ae14842405bb2462 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 22 Oct 2023 22:12:27 +0800 Subject: [PATCH] Fix incorrect ctx usage in defer function (#27740) --- services/task/migrate.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/services/task/migrate.go b/services/task/migrate.go index 475fa7a61..9cef77a6c 100644 --- a/services/task/migrate.go +++ b/services/task/migrate.go @@ -42,13 +42,11 @@ func handleCreateError(owner *user_model.User, err error) error { } func runMigrateTask(ctx context.Context, t *admin_model.Task) (err error) { - defer func() { + defer func(ctx context.Context) { if e := recover(); e != nil { err = fmt.Errorf("PANIC whilst trying to do migrate task: %v", e) log.Critical("PANIC during runMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d]: %v\nStacktrace: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, e, log.Stack(2)) } - // fixme: Because ctx is canceled here, so the db.DefaultContext is needed. - ctx := db.DefaultContext if err == nil { err = admin_model.FinishMigrateTask(ctx, t) if err == nil { @@ -69,7 +67,7 @@ func runMigrateTask(ctx context.Context, t *admin_model.Task) (err error) { } // then, do not delete the repository, otherwise the users won't be able to see the last error - }() + }(graceful.GetManager().ShutdownContext()) // even if the parent ctx is canceled, this defer-function still needs to update the task record in database if err = t.LoadRepo(ctx); err != nil { return err