Skip to content

Commit

Permalink
time: add a regression test for time.AfterFunc goroutine starvation
Browse files Browse the repository at this point in the history
The test is skipped on wasm platforms for now, because it
successfully detects a starvation bug on those platforms.

For golang#65178.

Change-Id: I05d28f1c7be99fcab67ec4dfaa38f412e11fd3cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/557038
Auto-Submit: Bryan Mills <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
Bryan C. Mills authored and ezz-no committed Feb 17, 2024
1 parent 0f62033 commit 7b7d4f7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/time/sleep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,50 @@ func TestAfterStress(t *testing.T) {
stop.Store(true)
}

func TestAfterFuncStarvation(t *testing.T) {
// Start two goroutines ping-ponging on a channel send.
// At any given time, at least one of these goroutines is runnable:
// if the channel buffer is full, the receiver is runnable,
// and if it is not full, the sender is runnable.
//
// In addition, the AfterFunc callback should become runnable after
// the indicated delay.
//
// Even if GOMAXPROCS=1, we expect the runtime to eventually schedule
// the AfterFunc goroutine instead of the runnable channel goroutine.
// However, in https://go.dev/issue/65178 this was observed to live-lock
// on wasip1/wasm and js/wasm after <10000 runs.

if runtime.GOARCH == "wasm" {
testenv.SkipFlaky(t, 65178)
}

defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))

var (
wg sync.WaitGroup
stop atomic.Bool
c = make(chan bool, 1)
)

wg.Add(2)
go func() {
for !stop.Load() {
c <- true
}
close(c)
wg.Done()
}()
go func() {
for range c {
}
wg.Done()
}()

AfterFunc(1*Microsecond, func() { stop.Store(true) })
wg.Wait()
}

func benchmark(b *testing.B, bench func(n int)) {

// Create equal number of garbage timers on each P before starting
Expand Down

0 comments on commit 7b7d4f7

Please sign in to comment.