Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 506c70f

Browse files
qiulaidongfenggopherbot
authored andcommitted
errgroup: propagate panic and Goexit through Wait
Recovered panic values are wrapped and saved in Group. Goexits are detected by a sentinel value set after the given function returns normally. Wait propagates the first instance of a panic or Goexit. According to the runtime.Goexit after the code will not be executed, with a bool, if f not call runtime.Goexit, is true, determine whether to propagate runtime.Goexit. Fixes golang/go#53757 Change-Id: Ic6426fc014fd1c4368ebaceef5b0d6163770a099 Reviewed-on: https://go-review.googlesource.com/c/sync/+/644575 Reviewed-by: Sean Liao <sean@liao.dev> Auto-Submit: Alan Donovan <adonovan@google.com> Commit-Queue: Alan Donovan <adonovan@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 396f3a0 commit 506c70f

File tree

2 files changed

+153
-18
lines changed

2 files changed

+153
-18
lines changed

errgroup/errgroup.go

+89-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ package errgroup
1212
import (
1313
"context"
1414
"fmt"
15+
"runtime"
16+
"runtime/debug"
1517
"sync"
1618
)
1719

@@ -31,6 +33,10 @@ type Group struct {
3133

3234
errOnce sync.Once
3335
err error
36+
37+
mu sync.Mutex
38+
panicValue any // = PanicError | PanicValue; non-nil if some Group.Go goroutine panicked.
39+
abnormal bool // some Group.Go goroutine terminated abnormally (panic or goexit).
3440
}
3541

3642
func (g *Group) done() {
@@ -50,13 +56,22 @@ func WithContext(ctx context.Context) (*Group, context.Context) {
5056
return &Group{cancel: cancel}, ctx
5157
}
5258

53-
// Wait blocks until all function calls from the Go method have returned, then
54-
// returns the first non-nil error (if any) from them.
59+
// Wait blocks until all function calls from the Go method have returned
60+
// normally, then returns the first non-nil error (if any) from them.
61+
//
62+
// If any of the calls panics, Wait panics with a [PanicValue];
63+
// and if any of them calls [runtime.Goexit], Wait calls runtime.Goexit.
5564
func (g *Group) Wait() error {
5665
g.wg.Wait()
5766
if g.cancel != nil {
5867
g.cancel(g.err)
5968
}
69+
if g.panicValue != nil {
70+
panic(g.panicValue)
71+
}
72+
if g.abnormal {
73+
runtime.Goexit()
74+
}
6075
return g.err
6176
}
6277

@@ -65,18 +80,56 @@ func (g *Group) Wait() error {
6580
// It blocks until the new goroutine can be added without the number of
6681
// active goroutines in the group exceeding the configured limit.
6782
//
68-
// The first call to return a non-nil error cancels the group's context, if the
69-
// group was created by calling WithContext. The error will be returned by Wait.
83+
// It blocks until the new goroutine can be added without the number of
84+
// goroutines in the group exceeding the configured limit.
85+
//
86+
// The first goroutine in the group that returns a non-nil error, panics, or
87+
// invokes [runtime.Goexit] will cancel the associated Context, if any.
7088
func (g *Group) Go(f func() error) {
7189
if g.sem != nil {
7290
g.sem <- token{}
7391
}
7492

93+
g.add(f)
94+
}
95+
96+
func (g *Group) add(f func() error) {
7597
g.wg.Add(1)
7698
go func() {
7799
defer g.done()
100+
normalReturn := false
101+
defer func() {
102+
if normalReturn {
103+
return
104+
}
105+
v := recover()
106+
g.mu.Lock()
107+
defer g.mu.Unlock()
108+
if !g.abnormal {
109+
if g.cancel != nil {
110+
g.cancel(g.err)
111+
}
112+
g.abnormal = true
113+
}
114+
if v != nil && g.panicValue == nil {
115+
switch v := v.(type) {
116+
case error:
117+
g.panicValue = PanicError{
118+
Recovered: v,
119+
Stack: debug.Stack(),
120+
}
121+
default:
122+
g.panicValue = PanicValue{
123+
Recovered: v,
124+
Stack: debug.Stack(),
125+
}
126+
}
127+
}
128+
}()
78129

79-
if err := f(); err != nil {
130+
err := f()
131+
normalReturn = true
132+
if err != nil {
80133
g.errOnce.Do(func() {
81134
g.err = err
82135
if g.cancel != nil {
@@ -101,19 +154,7 @@ func (g *Group) TryGo(f func() error) bool {
101154
}
102155
}
103156

104-
g.wg.Add(1)
105-
go func() {
106-
defer g.done()
107-
108-
if err := f(); err != nil {
109-
g.errOnce.Do(func() {
110-
g.err = err
111-
if g.cancel != nil {
112-
g.cancel(g.err)
113-
}
114-
})
115-
}
116-
}()
157+
g.add(f)
117158
return true
118159
}
119160

@@ -135,3 +176,33 @@ func (g *Group) SetLimit(n int) {
135176
}
136177
g.sem = make(chan token, n)
137178
}
179+
180+
// PanicError wraps an error recovered from an unhandled panic
181+
// when calling a function passed to Go or TryGo.
182+
type PanicError struct {
183+
Recovered error
184+
Stack []byte // result of call to [debug.Stack]
185+
}
186+
187+
func (p PanicError) Error() string {
188+
// A Go Error method conventionally does not include a stack dump, so omit it
189+
// here. (Callers who care can extract it from the Stack field.)
190+
return fmt.Sprintf("recovered from errgroup.Group: %v", p.Recovered)
191+
}
192+
193+
func (p PanicError) Unwrap() error { return p.Recovered }
194+
195+
// PanicValue wraps a value that does not implement the error interface,
196+
// recovered from an unhandled panic when calling a function passed to Go or
197+
// TryGo.
198+
type PanicValue struct {
199+
Recovered any
200+
Stack []byte // result of call to [debug.Stack]
201+
}
202+
203+
func (p PanicValue) String() string {
204+
if len(p.Stack) > 0 {
205+
return fmt.Sprintf("recovered from errgroup.Group: %v\n%s", p.Recovered, p.Stack)
206+
}
207+
return fmt.Sprintf("recovered from errgroup.Group: %v", p.Recovered)
208+
}

errgroup/errgroup_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"net/http"
1212
"os"
13+
"strings"
1314
"sync/atomic"
1415
"testing"
1516
"time"
@@ -289,6 +290,69 @@ func TestCancelCause(t *testing.T) {
289290
}
290291
}
291292

293+
func TestPanic(t *testing.T) {
294+
t.Run("error", func(t *testing.T) {
295+
g := &errgroup.Group{}
296+
p := errors.New("")
297+
g.Go(func() error {
298+
panic(p)
299+
})
300+
defer func() {
301+
err := recover()
302+
if err == nil {
303+
t.Fatalf("should propagate panic through Wait")
304+
}
305+
pe, ok := err.(errgroup.PanicError)
306+
if !ok {
307+
t.Fatalf("type should is errgroup.PanicError, but is %T", err)
308+
}
309+
if pe.Recovered != p {
310+
t.Fatalf("got %v, want %v", pe.Recovered, p)
311+
}
312+
if !strings.Contains(string(pe.Stack), "TestPanic.func") {
313+
t.Log(string(pe.Stack))
314+
t.Fatalf("stack trace incomplete")
315+
}
316+
}()
317+
g.Wait()
318+
})
319+
t.Run("any", func(t *testing.T) {
320+
g := &errgroup.Group{}
321+
g.Go(func() error {
322+
panic(1)
323+
})
324+
defer func() {
325+
err := recover()
326+
if err == nil {
327+
t.Fatalf("should propagate panic through Wait")
328+
}
329+
pe, ok := err.(errgroup.PanicValue)
330+
if !ok {
331+
t.Fatalf("type should is errgroup.PanicValue, but is %T", err)
332+
}
333+
if pe.Recovered != 1 {
334+
t.Fatalf("got %v, want %v", pe.Recovered, 1)
335+
}
336+
if !strings.Contains(string(pe.Stack), "TestPanic.func") {
337+
t.Log(string(pe.Stack))
338+
t.Fatalf("stack trace incomplete")
339+
}
340+
}()
341+
g.Wait()
342+
})
343+
}
344+
345+
func TestGoexit(t *testing.T) {
346+
g := &errgroup.Group{}
347+
g.Go(func() error {
348+
t.Skip()
349+
t.Fatalf("Goexit fail")
350+
return nil
351+
})
352+
g.Wait()
353+
t.Fatalf("should call runtime.Goexit from Wait")
354+
}
355+
292356
func BenchmarkGo(b *testing.B) {
293357
fn := func() {}
294358
g := &errgroup.Group{}

0 commit comments

Comments
 (0)