Source file src/runtime/testdata/testgoroutineleakprofile/goker/istio16224.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"runtime/pprof"
    10  	"sync"
    11  	"time"
    12  )
    13  
    14  func init() {
    15  	register("Istio16224", Istio16224)
    16  }
    17  
    18  type ConfigStoreCache_istio16224 interface {
    19  	RegisterEventHandler(handler func())
    20  	Run()
    21  }
    22  
    23  type Event_istio16224 int
    24  
    25  type Handler_istio16224 func(Event_istio16224)
    26  
    27  type configstoreMonitor_istio16224 struct {
    28  	handlers []Handler_istio16224
    29  	eventCh  chan Event_istio16224
    30  }
    31  
    32  func (m *configstoreMonitor_istio16224) Run(stop <-chan struct{}) {
    33  	for {
    34  		select {
    35  		case <-stop:
    36  			// This bug is not descibed, but is a true positive (in our eyes)
    37  			// In a real run main exits when the goro is blocked here.
    38  			if _, ok := <-m.eventCh; ok {
    39  				close(m.eventCh)
    40  			}
    41  			return
    42  		case ce, ok := <-m.eventCh:
    43  			if ok {
    44  				m.processConfigEvent(ce)
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func (m *configstoreMonitor_istio16224) processConfigEvent(ce Event_istio16224) {
    51  	m.applyHandlers(ce)
    52  }
    53  
    54  func (m *configstoreMonitor_istio16224) AppendEventHandler(h Handler_istio16224) {
    55  	m.handlers = append(m.handlers, h)
    56  }
    57  
    58  func (m *configstoreMonitor_istio16224) applyHandlers(e Event_istio16224) {
    59  	for _, f := range m.handlers {
    60  		f(e)
    61  	}
    62  }
    63  func (m *configstoreMonitor_istio16224) ScheduleProcessEvent(configEvent Event_istio16224) {
    64  	m.eventCh <- configEvent
    65  }
    66  
    67  type Monitor_istio16224 interface {
    68  	Run(<-chan struct{})
    69  	AppendEventHandler(Handler_istio16224)
    70  	ScheduleProcessEvent(Event_istio16224)
    71  }
    72  
    73  type controller_istio16224 struct {
    74  	monitor Monitor_istio16224
    75  }
    76  
    77  func (c *controller_istio16224) RegisterEventHandler(f func(Event_istio16224)) {
    78  	c.monitor.AppendEventHandler(f)
    79  }
    80  
    81  func (c *controller_istio16224) Run(stop <-chan struct{}) {
    82  	c.monitor.Run(stop)
    83  }
    84  
    85  func (c *controller_istio16224) Create() {
    86  	c.monitor.ScheduleProcessEvent(Event_istio16224(0))
    87  }
    88  
    89  func NewMonitor_istio16224() Monitor_istio16224 {
    90  	return NewBufferedMonitor_istio16224()
    91  }
    92  
    93  func NewBufferedMonitor_istio16224() Monitor_istio16224 {
    94  	return &configstoreMonitor_istio16224{
    95  		eventCh: make(chan Event_istio16224),
    96  	}
    97  }
    98  
    99  func Istio16224() {
   100  	prof := pprof.Lookup("goroutineleak")
   101  	defer func() {
   102  		time.Sleep(100 * time.Millisecond)
   103  		prof.WriteTo(os.Stdout, 2)
   104  	}()
   105  
   106  	for i := 0; i < 100; i++ {
   107  		go func() {
   108  			controller := &controller_istio16224{monitor: NewMonitor_istio16224()}
   109  			done := make(chan bool)
   110  			lock := sync.Mutex{}
   111  			controller.RegisterEventHandler(func(event Event_istio16224) {
   112  				lock.Lock()
   113  				defer lock.Unlock()
   114  				done <- true
   115  			})
   116  
   117  			stop := make(chan struct{})
   118  			go controller.Run(stop)
   119  
   120  			controller.Create()
   121  
   122  			lock.Lock() // blocks
   123  			lock.Unlock()
   124  			<-done
   125  
   126  			close(stop)
   127  		}()
   128  	}
   129  }
   130  

View as plain text