fixed unhandled panic and wrapped all internal goroutines
This commit is contained in:
+2
-2
@@ -193,7 +193,7 @@ func (c *Cron) Start() {
|
||||
c.runDue(time.Now())
|
||||
|
||||
// run after each tick
|
||||
go func() {
|
||||
routine.FireAndForget(func() {
|
||||
for {
|
||||
select {
|
||||
case <-c.tickerDone:
|
||||
@@ -202,7 +202,7 @@ func (c *Cron) Start() {
|
||||
c.runDue(t)
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
})
|
||||
c.mux.Unlock()
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/routine"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
@@ -339,7 +340,7 @@ func (u *Uploader) multipartUpload(ctx context.Context, initPart []byte, optReqF
|
||||
if len(initPart) != 0 {
|
||||
totalWorkers--
|
||||
initPartNumber := u.lastPartNumber
|
||||
g.Go(func() error {
|
||||
g.Go(routine.SafeWrap(func() error {
|
||||
mp, err := u.uploadPart(ctx, initPartNumber, initPart, optReqFuncs...)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -350,13 +351,13 @@ func (u *Uploader) multipartUpload(ctx context.Context, initPart []byte, optReqF
|
||||
u.mu.Unlock()
|
||||
|
||||
return nil
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
totalWorkers = max(totalWorkers, 1)
|
||||
|
||||
for i := 0; i < totalWorkers; i++ {
|
||||
g.Go(func() error {
|
||||
g.Go(routine.SafeWrap(func() error {
|
||||
for {
|
||||
part, num, err := u.nextPart()
|
||||
if err != nil {
|
||||
@@ -377,7 +378,7 @@ func (u *Uploader) multipartUpload(ctx context.Context, initPart []byte, optReqF
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
return g.Wait()
|
||||
|
||||
@@ -42,6 +42,7 @@ import (
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/filesystem/blob"
|
||||
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
|
||||
"github.com/pocketbase/pocketbase/tools/routine"
|
||||
)
|
||||
|
||||
const defaultPageSize = 1000
|
||||
@@ -359,7 +360,7 @@ func (w *writer) Write(p []byte) (int, error) {
|
||||
// error uploading to S3.
|
||||
func (w *writer) open(r io.Reader, closePipeOnError bool) {
|
||||
// This goroutine will keep running until Close, unless there's an error.
|
||||
go func() {
|
||||
routine.FireAndForget(func() {
|
||||
defer func() {
|
||||
close(w.donec)
|
||||
}()
|
||||
@@ -378,7 +379,7 @@ func (w *writer) open(r io.Reader, closePipeOnError bool) {
|
||||
}
|
||||
w.err = err
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
// Close completes the writer and closes it. Any error occurring during write
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package routine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
"sync"
|
||||
@@ -33,3 +34,17 @@ func FireAndForget(f func(), wg ...*sync.WaitGroup) {
|
||||
f()
|
||||
}()
|
||||
}
|
||||
|
||||
// SafeWrap wraps the provided function with auto panic recover handling
|
||||
// and returns any eventual panic as regular error.
|
||||
func SafeWrap(f func() error) func() error {
|
||||
return func() (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("[SafeWrap] recovered from panic: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
return f()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package routine_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -12,7 +13,7 @@ func TestFireAndForget(t *testing.T) {
|
||||
|
||||
fn := func() {
|
||||
called = true
|
||||
panic("test")
|
||||
panic("test_recover")
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
@@ -22,6 +23,29 @@ func TestFireAndForget(t *testing.T) {
|
||||
wg.Wait()
|
||||
|
||||
if !called {
|
||||
t.Error("Expected fn to be called.")
|
||||
t.Fatal("Expected fn to be called.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeWrap(t *testing.T) {
|
||||
called := false
|
||||
|
||||
fn := func() error {
|
||||
called = true
|
||||
panic("test_recover")
|
||||
}
|
||||
|
||||
err := routine.SafeWrap(fn)()
|
||||
|
||||
if !called {
|
||||
t.Fatal("Expected fn to be called.")
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Expected fn panic to be converted to error")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "test_recover") {
|
||||
t.Fatal("Expected the returned error to contain the recovered panic value")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/tools/dbutils"
|
||||
"github.com/pocketbase/pocketbase/tools/inflector"
|
||||
"github.com/pocketbase/pocketbase/tools/routine"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
@@ -336,8 +337,8 @@ func (s *Provider) Exec(items any) (*Result, error) {
|
||||
if !s.skipTotal {
|
||||
// execute the 2 queries concurrently
|
||||
errg := new(errgroup.Group)
|
||||
errg.Go(countExec)
|
||||
errg.Go(modelsExec)
|
||||
errg.Go(routine.SafeWrap(countExec))
|
||||
errg.Go(routine.SafeWrap(modelsExec))
|
||||
if err := errg.Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user