Skip to content

Commit f978b8f

Browse files
committed
pool: use simple defer recover
1 parent 7e17b9e commit f978b8f

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

pool/context_pool.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package pool
22

33
import (
44
"context"
5-
6-
"github.com/sourcegraph/conc/panics"
75
)
86

97
// ContextPool is a pool that runs tasks that take a context.
@@ -25,31 +23,26 @@ type ContextPool struct {
2523
// are busy, a call to Go() will block until the task can be started.
2624
func (p *ContextPool) Go(f func(ctx context.Context) error) {
2725
p.errorPool.Go(func() error {
28-
// If we aren't cancelling on error, just return the result of f.
29-
if !p.cancelOnError {
30-
return f(p.ctx)
26+
if p.cancelOnError {
27+
// If we are cancelling on error, then we also want to cancel if a
28+
// panic is raised. To do this, we need to recover, cancel, and then
29+
// re-throw the caught panic.
30+
defer func() {
31+
if r := recover(); r != nil {
32+
p.cancel()
33+
panic(r)
34+
}
35+
}()
3136
}
3237

33-
// If we are cancelling on error, then we also want to cancel on panic.
34-
// To do this, we need to recover from any panic f raises.
35-
var err error
36-
recovered := panics.Try(func() { err = f(p.ctx) })
37-
if err != nil || recovered != nil {
38+
err := f(p.ctx)
39+
if err != nil && p.cancelOnError {
3840
// Leaky abstraction warning: We add the error directly because
3941
// otherwise, canceling could cause another goroutine to exit and
4042
// return an error before this error was added, which breaks the
4143
// expectations of WithFirstError().
42-
if err != nil {
43-
p.errorPool.addErr(err)
44-
}
45-
44+
p.errorPool.addErr(err)
4645
p.cancel()
47-
48-
// Now that context is cancelled, if we caught a panic we can
49-
// propagate it.
50-
if recovered != nil {
51-
panic(recovered)
52-
}
5346
return nil
5447
}
5548
return err

0 commit comments

Comments
 (0)