diff --git a/services/cron/tasks.go b/services/cron/tasks.go index d2c3d1d812..f0956a97d8 100644 --- a/services/cron/tasks.go +++ b/services/cron/tasks.go @@ -84,13 +84,15 @@ func (t *Task) RunWithUser(doer *user_model.User, config Config) { t.lock.Unlock() defer func() { taskStatusTable.Stop(t.Name) - if err := recover(); err != nil { - // Recover a panic within the - combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2)) - log.Error("PANIC whilst running task: %s Value: %v", t.Name, combinedErr) - } }() graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) { + defer func() { + if err := recover(); err != nil { + // Recover a panic within the execution of the task. + combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2)) + log.Error("PANIC whilst running task: %s Value: %v", t.Name, combinedErr) + } + }() // Store the time of this run, before the function is executed, so it // matches the behavior of what the cron library does. t.lock.Lock()