neilotoole / errgroup

errgroup with goroutine worker limits

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

There is no recover in startG

richzw opened this issue · comments

There is no recover in startG, so it could cause a process crash once the goroutine panic.

Maybe we could solve it through

func (g *Group) startG() {
	g.wg.Add(1)
	go func() {
		defer func() {
			if r := recover(); r != nil {
				buf := make([]byte, 64<<10)
				buf = buf[:runtime.Stack(buf, false)]
				g.err = fmt.Errorf("errgroup: panic recovered: %s\n %s", r, buf)
			}
			g.wg.Done()
		}()

		var f func() error

@richzw It's a goal to emulate the behavior of sync/errgroup as much as possible. And that doesn't handle panics for you either... I'm not sure it's desirable to do so.

From sync/errgroup:

// Go calls the given function in a new goroutine.
//
// The first call to return a non-nil error cancels the group; its error will be
// returned by Wait.
func (g *Group) Go(f func() error) {
	g.wg.Add(1)

	go func() {
		defer g.wg.Done()

		if err := f(); err != nil {
			g.errOnce.Do(func() {
				g.err = err
				if g.cancel != nil {
					g.cancel()
				}
			})
		}
	}()
}

I'm open to suggestions about it, but all other things being equal, emulation of sync/errgroup is the goal.