Skip to content

Commit

Permalink
fix: parallel panic use recover (#768)
Browse files Browse the repository at this point in the history
Co-authored-by: cc <cc@xinpianchang.com>
  • Loading branch information
imlewc and cc committed Sep 5, 2023
1 parent 7f51495 commit 5f12697
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/parallel/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package parallel

import (
"github.com/zhenghaoz/gorse/base"
"sync"

"github.com/juju/errors"
Expand Down Expand Up @@ -56,6 +57,7 @@ func Parallel(nJobs, nWorkers int, worker func(workerId, jobId int) error) error
for j := 0; j < nWorkers; j++ {
// start workers
go func(workerId int) {
defer base.CheckPanic()
defer wg.Done()
for {
// read job
Expand Down
11 changes: 11 additions & 0 deletions base/parallel/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,14 @@ func TestSplit(t *testing.T) {
b = Split(a, 3)
assert.Equal(t, [][]int{{1, 2, 3}, {4, 5}, {6, 7}}, b)
}

// check panic
func TestParallelPanic(t *testing.T) {
err := Parallel(10000, 4, func(workerId, jobId int) error {
panic("panic")
})
if err != nil {
return
}
time.Sleep(time.Second * 3)
}

0 comments on commit 5f12697

Please sign in to comment.