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: sync: add WaitGroup.Notify to use with select #71231

Open
mohamedattahri opened this issue Jan 11, 2025 · 7 comments
Open

proposal: sync: add WaitGroup.Notify to use with select #71231

mohamedattahri opened this issue Jan 11, 2025 · 7 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@mohamedattahri
Copy link

mohamedattahri commented Jan 11, 2025

Background

Integrating WaitGroup with select statements, especially in context-aware operations where cancellation might be necessary, requires creating an additional channel for signaling.

Proposal

I propose extending sync.WaitGroup with a new method:

func (wg *WaitGroup) Notify() <- chan struct{}

This method would return a channel that is closed when WaitGroup.Wait returns.

Example

Instead of:

c := make(chan struct{})
go func() {
    wg.Wait()
    close(c)
}()

select {
case <-ctx.Done():
    return context.Cause(ctx)
case <-c:
    return nil
}

You could do:

select {
case <- ctx.Done():
    return context.Cause(ctx)
case <- wg.Notify():
    return nil
}

Conclusion

This boilerplate it replaces is small, but I believe it does a lot to bring WaitGroup closer to the built-in concurrency tools offered by the language.

CC #71076.

@gopherbot gopherbot added this to the Proposal milestone Jan 11, 2025
@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Jan 11, 2025
@zigo101
Copy link

zigo101 commented Jan 11, 2025

@seankhliao seankhliao changed the title proposal: sync: add Notify method to sync.WaitGroup proposal: sync: add WaitGroup.Notify to use with select Jan 11, 2025
@zephyrtronium
Copy link
Contributor

zephyrtronium commented Jan 11, 2025

I think this is a strictly worse version of what x/sync/errgroup does. #40916 (comment) is exactly my sentiment:

In most cases when you have the possibility of a timeout, you want to cancel the work rather than just the action of waiting on the work, and you want to distinguish between “work completed” (err != nil) and “work timed out” (err == nil). For those cases, errgroup.WithContext is usually a better fit.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Jan 12, 2025
@zigo101
Copy link

zigo101 commented Jan 12, 2025

This is a very basic package, don't let Context pollute it.

@krishpranav
Copy link

Hi, so do you want to implement the new functionality?

@mohamedattahri
Copy link
Author

@zephyrtronium - I wouldn't compare them one to one.

I agree that errgroup offers a much better way to handle context cancellation, but there are many cases where a WaitGroup is one of many concurrent things you're managing.

In my view, this makes compatibility with the select statement a desirable feature.

I'd even argue that errgroup.Group should feature an analog method — with a channel that returns an error of course.

@zigo101 — There is no context involved here.

@zigo101
Copy link

zigo101 commented Jan 13, 2025

I mean errgroup.WithContext introduces Context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
Status: Incoming
Development

No branches or pull requests

6 participants