// WEAVE_META
// outcome: leak
// concurrency_pattern: mutex
// goroutine_count: 3
// expected_nondeterminism: medium
// description: Human-written GoBench bug kernel cockroach_1055 (leak)

package main

import (
	"os"
	"runtime/trace"
	"sync"
	"sync/atomic"
	)

type Stopper struct {
	stopper  chan struct{}
	stop     sync.WaitGroup
	mu       sync.Mutex
	draining int32
	drain    sync.WaitGroup
}

func (s *Stopper) AddWorker() {
	s.stop.Add(1)
}

func (s *Stopper) ShouldStop() <-chan struct{} {
	if s == nil {
		return nil
	}
	return s.stopper
}

func (s *Stopper) SetStopped() {
	if s != nil {
		s.stop.Done()
	}
}

func (s *Stopper) Quiesce() {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.draining = 1
	s.drain.Wait()
	s.draining = 0
}

func (s *Stopper) Stop() {
	s.mu.Lock()
	defer s.mu.Unlock()
	atomic.StoreInt32(&s.draining, 1)
	s.drain.Wait()
	close(s.stopper)
	s.stop.Wait()
}

func (s *Stopper) StartTask() bool {
	if atomic.LoadInt32(&s.draining) == 0 {
		s.mu.Lock()
		defer s.mu.Unlock()
		s.drain.Add(1)
		return true
	}
	return false
}

func NewStopper() *Stopper {
	return &Stopper{
		stopper: make(chan struct{}),
	}
}

func main() {
	if tf := os.Getenv("WEAVE_TRACE_FILE"); tf != "" {
		f, err := os.Create(tf)
		if err == nil {
			if err := trace.Start(f); err == nil {
				defer func() { trace.Stop(); f.Close() }()
			}
		}
	}

	var stoppers []*Stopper
	for i := 0; i < 3; i++ {
		stoppers = append(stoppers, NewStopper())
	}

	for i := range stoppers {
		s := stoppers[i]
		s.AddWorker()
		go func() {
			s.StartTask()
			<-s.ShouldStop()
			s.SetStopped()
		}()
	}

	done := make(chan struct{})
	go func() {
		for _, s := range stoppers {
			s.Quiesce()
		}
		for _, s := range stoppers {
			s.Stop()
		}
		close(done)
	}()

	<-done
}


Outcome: leak
Concurrency pattern: mutex
Goroutine count: 3
Nondeterminism: medium
Timed out: yes

Partial scheduler trace:

Race detector output:
==================
WARNING: DATA RACE
Read at 0x00c00019a060 by goroutine 10:
  ??()
      -:0 +0x102c24df8
  sync/atomic.LoadInt32()
      <autogenerated>:1 +0x10
  main.main.func2()
      /Users/kaviruhapuarachchi/Downloads/Weave/programs/goker_cockroach_1055.go:94 +0x2c

Previous write at 0x00c00019a060 by goroutine 13:
  main.(*Stopper).Quiesce()
      /Users/kaviruhapuarachchi/Downloads/Weave/programs/goker_cockroach_1055.go:45 +0x7c
  main.main.func3()
      /Users/kaviruhapuarachchi/Downloads/Weave/programs/goker_cockroach_1055.go:103 +0x58

Goroutine 10 (running) created at:
  main.main()
      /Users/kaviruhapuarachchi/Downloads/Weave/programs/goker_cockroach_1055.go:93 +0x218

Goroutine 13 (running) created at:
  main.main()
      /Users/kaviruhapuarachchi/Downloads/Weave/programs/goker_cockroach_1055.go:101 +0x374
==================