Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes58107.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  /*
     6   * Project: kubernetes
     7   * Tag: Reproduce misbehavior
     8   * Issue or PR  : https://github.com/kubernetes/kubernetes/pull/58107
     9   * Buggy version: 2f17d782eb2772d6401da7ddced9ac90656a7a79
    10   * fix commit-id: 010a127314a935d8d038f8dd4559fc5b249813e4
    11   * Flaky: 53/100
    12   */
    13  
    14  package main
    15  
    16  import (
    17  	"os"
    18  	"runtime"
    19  	"runtime/pprof"
    20  	"sync"
    21  	"time"
    22  )
    23  
    24  func init() {
    25  	register("Kubernetes58107", Kubernetes58107)
    26  }
    27  
    28  type RateLimitingInterface_kubernetes58107 interface {
    29  	Get()
    30  	Put()
    31  }
    32  
    33  type Type_kubernetes58107 struct {
    34  	cond *sync.Cond
    35  }
    36  
    37  func (q *Type_kubernetes58107) Get() {
    38  	q.cond.L.Lock()
    39  	defer q.cond.L.Unlock()
    40  	q.cond.Wait()
    41  }
    42  
    43  func (q *Type_kubernetes58107) Put() {
    44  	q.cond.Signal()
    45  }
    46  
    47  type ResourceQuotaController_kubernetes58107 struct {
    48  	workerLock        sync.RWMutex
    49  	queue             RateLimitingInterface_kubernetes58107
    50  	missingUsageQueue RateLimitingInterface_kubernetes58107
    51  }
    52  
    53  func (rq *ResourceQuotaController_kubernetes58107) worker(queue RateLimitingInterface_kubernetes58107, _ string) {
    54  	workFunc := func() bool {
    55  		rq.workerLock.RLock()
    56  		defer rq.workerLock.RUnlock()
    57  		queue.Get()
    58  		return true
    59  	}
    60  	for {
    61  		if quit := workFunc(); quit {
    62  			return
    63  		}
    64  	}
    65  }
    66  
    67  func (rq *ResourceQuotaController_kubernetes58107) Run() {
    68  	go rq.worker(rq.queue, "G1")             // G3
    69  	go rq.worker(rq.missingUsageQueue, "G2") // G4
    70  }
    71  
    72  func (rq *ResourceQuotaController_kubernetes58107) Sync() {
    73  	for i := 0; i < 100000; i++ {
    74  		rq.workerLock.Lock()
    75  		runtime.Gosched()
    76  		rq.workerLock.Unlock()
    77  	}
    78  }
    79  
    80  func (rq *ResourceQuotaController_kubernetes58107) HelperSignals() {
    81  	for i := 0; i < 100000; i++ {
    82  		rq.queue.Put()
    83  		rq.missingUsageQueue.Put()
    84  	}
    85  }
    86  
    87  func startResourceQuotaController_kubernetes58107() {
    88  	resourceQuotaController := &ResourceQuotaController_kubernetes58107{
    89  		queue:             &Type_kubernetes58107{sync.NewCond(&sync.Mutex{})},
    90  		missingUsageQueue: &Type_kubernetes58107{sync.NewCond(&sync.Mutex{})},
    91  	}
    92  
    93  	go resourceQuotaController.Run()  // G2
    94  	go resourceQuotaController.Sync() // G5
    95  	resourceQuotaController.HelperSignals()
    96  }
    97  
    98  func Kubernetes58107() {
    99  	prof := pprof.Lookup("goroutineleak")
   100  	defer func() {
   101  		time.Sleep(1000 * time.Millisecond)
   102  		prof.WriteTo(os.Stdout, 2)
   103  	}()
   104  
   105  	for i := 0; i < 1000; i++ {
   106  		go startResourceQuotaController_kubernetes58107() // G1
   107  	}
   108  }
   109  

View as plain text