summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorEarl Warren <109468362+earl-warren@users.noreply.github.com>2023-12-10 21:15:06 +0100
committerGitHub <noreply@github.com>2023-12-10 20:15:06 +0000
commitfab73e4a3a598aa9433522f27edf2d8efea3d0e0 (patch)
tree6ed02477670bbfa57f59dfd402bb877834f59863 /services
parent9a99c17a1412fce29944bd9a87d7dc6d40423c77 (diff)
downloadgitea-fab73e4a3a598aa9433522f27edf2d8efea3d0e0.tar.gz
gitea-fab73e4a3a598aa9433522f27edf2d8efea3d0e0.zip
Actually recover from a panic in cron task (#28409)
- 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: Gusted <postmaster@gusted.xyz>
Diffstat (limited to 'services')
-rw-r--r--services/cron/tasks.go12
1 files changed, 7 insertions, 5 deletions
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()