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

proposal: x/sync/errgroup: Additional goroutines should not be started after the context is cancelled #61611

Closed
nbgraham opened this issue Jul 27, 2023 · 1 comment
Labels
Milestone

Comments

@nbgraham
Copy link

Once a goroutine is ready to run (no longer blocked), errgroup should check if the context has already been cancelled (e.g. the errgroup was created with WithContext and a previous step returned an error). If it has, the next step should not be run.

I guess we would have to check g.cancel != nil to check if the errgroup was created with WithContext to preserve the existing behavior: if not using WithContext, all goroutines should be completed regardless of errors being returned.

(*Group) Go

Add this

if g.cancel != nil  && g.err != nil {
  return
}

here
https://github.com/golang/sync/blob/93782cc822b6b554cb7df40332fd010f0473cbc8/errgroup/errgroup.go#L70

This change might be problematic, because it would mean that calling Go does not guarantee that the function will run. Might have to return a bool or error from the Go func.

(*Group) TryGo

Add this

if g.cancel != nil  && g.err != nil {
  return false
}

here
https://github.com/golang/sync/blob/93782cc822b6b554cb7df40332fd010f0473cbc8/errgroup/errgroup.go#L99

Workaround

I can achieve this behavior today like this, but it feels like something that should be included.

	g, ctx := errgroup.WithContext(context.Background()) // NEW
	g.SetLimit(1) // NEW
	var urls = []string{
		"http://www.golang.org/",
		"http://www.google.com/",
		"http://www.somestupidname.com/",
	}
	for _, url := range urls {
		// Launch a goroutine to fetch the URL.
		url := url // https://golang.org/doc/faq#closures_and_goroutines
		g.Go(func() error {
			// NEW
			if err := ctx.Err(); err != nil {
				return err
			}

			// Fetch the URL.
			resp, err := http.Get(url)
			if err == nil {
				resp.Body.Close()
			}
			return err
		})
	}
	// Wait for all HTTP fetches to complete.
	if err := g.Wait(); err == nil {
		fmt.Println("Successfully fetched all URLs.")
	}
@gopherbot gopherbot added this to the Proposal milestone Jul 27, 2023
@seankhliao
Copy link
Member

Duplicate of #54045

@seankhliao seankhliao marked this as a duplicate of #54045 Jul 27, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants