Skip to content

sync: spurious wakeup from WaitGroup.Wait #7734

Closed
@rui314

Description

@rui314
What does 'go version' print?
go version devel +519230b4d06a Sun Apr 06 10:30:02 2014 -0400 + linux/amd64

What steps reproduce the problem?
The following program, which uses a WaitGroup to wait for two goroutines to complete,
should never exit.

package main

import (
        "fmt"
        "runtime"
        "sync"
        "sync/atomic"
)

func main() {
        for i := int64(0); ; i++ {
                wg := &sync.WaitGroup{}
                wg.Add(1)
                counter := int32(0)
                // spawn goroutine 1                                                                                                                        
                go func() {
                        atomic.AddInt32(&counter, 1)
                        wg.Done()
                }()
                runtime.Gosched()
                wg.Add(1)
                // spawn goroutine 2                                                                                                                        
                go func() {
                        atomic.AddInt32(&counter, 1)
                        wg.Done()
                }()
                // Wait for goroutine 1 and 2                                                                                                                   
                wg.Wait()
                if atomic.LoadInt32(&counter) != 2 {
                        fmt.Println(i, "Spurious wakeup from Wait()")
                        return
                }
        }
}

What happened?
It exits because of a bug in WaitGroup. (It may take a few minutes to fail.)

$ GOMAXPROCS=2 go run test.go
26994961 Spurious wakeup from Wait()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions