Skip to content

Commit 6f4d5c0

Browse files
GiteaBotearl-warrenGusted
authored
Recover from panic in cron task (#28409) (#28425)
Backport #28409 by @earl-warren - Currently there's code to recover gracefully from panics that happen within the execution of cron tasks. However this recover code wasn't being run, because `RunWithShutdownContext` also contains code to recover from any panic and then gracefully shutdown Forgejo. Because `RunWithShutdownContext` registers that code as last, that would get run first which in this case is not behavior that we want. - Move the recover code to inside the function, so that is run first before `RunWithShutdownContext`'s recover code (which is now a noop). Fixes: https://codeberg.org/forgejo/forgejo/issues/1910 Co-authored-by: Earl Warren <[email protected]> Co-authored-by: Gusted <[email protected]>
1 parent 1ec622d commit 6f4d5c0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

services/cron/tasks.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ func (t *Task) RunWithUser(doer *user_model.User, config Config) {
8484
t.lock.Unlock()
8585
defer func() {
8686
taskStatusTable.Stop(t.Name)
87-
if err := recover(); err != nil {
88-
// Recover a panic within the
89-
combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
90-
log.Error("PANIC whilst running task: %s Value: %v", t.Name, combinedErr)
91-
}
9287
}()
9388
graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) {
89+
defer func() {
90+
if err := recover(); err != nil {
91+
// Recover a panic within the execution of the task.
92+
combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
93+
log.Error("PANIC whilst running task: %s Value: %v", t.Name, combinedErr)
94+
}
95+
}()
9496
// Store the time of this run, before the function is executed, so it
9597
// matches the behavior of what the cron library does.
9698
t.lock.Lock()

0 commit comments

Comments
 (0)