Skip to content

min0625/errgroup

Repository files navigation

errgroup

Go Reference codecov

English | 繁體中文

A drop-in golang.org/x/sync/errgroup replacement that recovers panics in goroutines and re-panics them inside the Wait call.

Ref: golang/go#53757

Packages

Package Description
github.com/min0625/errgroup Drop-in replacement for golang.org/x/sync/errgroup with panic recovery
github.com/min0625/errgroup/x/errgroup Context-aware variant — passes context.Context directly into each goroutine function

Installation

go get github.com/min0625/errgroup

Example

package main

import (
	"fmt"

	"github.com/min0625/errgroup"
)

func main() {
	// This case uses "github.com/min0625/errgroup" which will catch panics.
	// If you import "golang.org/x/sync/errgroup" instead, it won't catch panics.
	// You can try this in the Go Playground: https://go.dev/play/p/7pUX6uQ2mCH
	var g errgroup.Group

	defer func() {
		// Will catch the panic.
		if p := recover(); p != nil {
			switch t := p.(type) {
			case errgroup.PanicValue:
				fmt.Println(t.Recovered)
			case errgroup.PanicError:
				fmt.Println(t.Recovered)
			}
		}
	}()

	g.Go(func() error {
		// Do something
		return nil
	})

	g.Go(func() error {
		panic("oops")
	})

	if err := g.Wait(); err != nil {
		// Handle error
		fmt.Println(err)
		return
	}

	// Output: oops
}

Panic behaviour

Panics in goroutines started by Go or TryGo are caught and re-panicked inside Wait, wrapped as:

  • PanicError — when the panicked value implements error
  • PanicValue — for all other values

Both types expose a Stack field containing the stack trace captured at the point of the panic.

If multiple goroutines panic concurrently, only the first panic is propagated; the rest are silently discarded.

About

Drop-in golang.org/x/sync/errgroup replacement that recovers goroutine panics and re-panics them in Wait.

Topics

Resources

License

Stars

Watchers

Forks

Contributors