Potential Issue in errgroup

22 views
Skip to first unread message

Icey

unread,
8:43 AM (13 hours ago) 8:43 AM
to golang-nuts
In errgroup, the step to increment the WaitGroup counter (wg.Add) is written after acquiring a token from the semaphore (sem). Is it possible that if all goroutines finish execution and call wg.Done at nearly the same time, the currently blocked goroutines do not immediately call wg.Add due to scheduling issues, resulting in the WaitGroup counter dropping to zero and causing wg.Wait() to return prematurely, leading to errors in the program?  


Axel Wagner

unread,
8:58 AM (13 hours ago) 8:58 AM
to Icey, golang-nuts
(sorry, didn't hit "Reply All", apparently)

Hi,

I don't believe the semaphore has anything to do with the wait group counter. So I don't believe it is relevant to this question. Note that a goroutine can be interrupted at any time anyways, so whether or not that code is there doesn't matter for correctness.

At that point, the issue comes down to "if you call Go and Wait concurrently, may Wait return before the function passed to Go finishes" and the answer to that is yes. That is analogous to calling wg.Add and wg.Wait concurrently for a sync.WaitGroup and it is valid for that to execute as if the Wait has been called, returned and then Add is called.

The way to avoid that is to only call Wait after all the Go calls have returned. That is, to not call Wait and Go concurrently.

On Tue, 2 Jul 2024 at 14:43, Icey <icey91...@gmail.com> wrote:
In errgroup, the step to increment the WaitGroup counter (wg.Add) is written after acquiring a token from the semaphore (sem). Is it possible that if all goroutines finish execution and call wg.Done at nearly the same time, the currently blocked goroutines do not immediately call wg.Add due to scheduling issues, resulting in the WaitGroup counter dropping to zero and causing wg.Wait() to return prematurely, leading to errors in the program?  


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/22ceece2-843c-47d6-ab5b-b5ddc5e51176n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages