Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can panic-recover handling add to the function Group.Go()? #83

Open
guodongq opened this issue Sep 8, 2023 · 1 comment
Open

Can panic-recover handling add to the function Group.Go()? #83

guodongq opened this issue Sep 8, 2023 · 1 comment

Comments

@guodongq
Copy link

guodongq commented Sep 8, 2023

When the program is processed in multiple goroutines, panic may occur, which will cause main goroutine to crash directly. Can following logic add to Group.Go() ?

// Go calls the given function in a new goroutine.
//
// If the function returns an error it is added to the group multierror which
// is returned by Wait.
func (g *Group) Go(f func() error) {
	g.wg.Add(1)

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

+		defer func() {
+			if r := recover(); r != nil {
+				g.mutex.Lock()
+				g.err = Append(g.err, fmt.Errorf("%v", r))
+				g.mutex.Unlock()
+			}
+		}()

		if err := f(); err != nil {
			g.mutex.Lock()
			g.err = Append(g.err, err)
			g.mutex.Unlock()
		}
	}()
}
@dolmen
Copy link

dolmen commented Mar 24, 2024

@guodongq You might be interested in my module github.com/dolmen-go/rendezvous that has this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants