Skip to content

x/sync/errgroup: clarify that Go calls the function regardless of prior errors #54045

@YairHalberstadt

Description

@YairHalberstadt

What version of Go are you using (go version)?

go version go1.19-pre4 cl/455575533 +12f49fe0ed linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package test

import (
	"errors"
	"sync/atomic"
	"testing"
	"time"

	"golang.org/x/sync/errgroup"
)

func TestErrGroupDoesNotRunNewFunctionsAfterPreviousError(t *testing.T) {
	group := errgroup.Group{}
	group.SetLimit(1)
	group.Go(func() error {
		return errors.New("")
	})
	time.Sleep(time.Second)

	var ran atomic.Bool
	group.Go(func() error {
		ran.Store(true)
		return nil
	})
        _ = group.Wait()
	if ran.Load() {
		t.Errorf("Expected second func not to run, but it did")
	}
}

What did you expect to see?

The documentation for errgroup states:

Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.

The first call to return a non-nil error cancels the group; its error will be returned by Wait.

I would assume that if the first call to return a non-nil error cancels the group, future funcs passed to go won't run.

Furthermore calls to group.Wait() should return immediately rather than waiting for funcs created after the group was cancelled to complete.

What did you see instead?

All funcs are called, even those created after the group was cancelled. group.Wait() waits for all funcs to complete before returning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.help wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions