Skip to content

proposal: x/sync: add panicgroup struct to deal with panic in the sub-goroutine #49802

Closed
@Zh1Cheung

Description

@Zh1Cheung

What did you do?

In Go,Unable to receive the panic of the sub-goroutine in the parent-goroutine.

func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("recover()")
		}
		return
	}()
	g, _ := errgroup.WithContext(context.Background())
	g.Go(func() error {
		panic("this is a panic")
	})

	if err := g.Wait(); err != nil {
		fmt.Println("occur err")
		return
	}
	time.Sleep(500 * time.Millisecond)
	fmt.Println("success")
	return
}

output:

panic: this is a panic

goroutine 6 [running]:

...
...

What did you expect to see?

What I hope is that there can be a structure similar to errgroup, like panicgroup to deal with the appearance of panic in the sub-goroutine.

PS

I can implement this function if needed

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions