Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes10182.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   * Issue or PR  : https://github.com/kubernetes/kubernetes/pull/10182
     8   * Buggy version: 4b990d128a17eea9058d28a3b3688ab8abafbd94
     9   * fix commit-id: 64ad3e17ad15cd0f9a4fd86706eec1c572033254
    10   * Flaky: 15/100
    11   */
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"runtime"
    17  	"runtime/pprof"
    18  	"sync"
    19  	"time"
    20  )
    21  
    22  func init() {
    23  	register("Kubernetes10182", Kubernetes10182)
    24  }
    25  
    26  type statusManager_kubernetes10182 struct {
    27  	podStatusesLock  sync.RWMutex
    28  	podStatusChannel chan bool
    29  }
    30  
    31  func (s *statusManager_kubernetes10182) Start() {
    32  	go func() {
    33  		for i := 0; i < 2; i++ {
    34  			s.syncBatch()
    35  		}
    36  	}()
    37  }
    38  
    39  func (s *statusManager_kubernetes10182) syncBatch() {
    40  	runtime.Gosched()
    41  	<-s.podStatusChannel
    42  	s.DeletePodStatus()
    43  }
    44  
    45  func (s *statusManager_kubernetes10182) DeletePodStatus() {
    46  	s.podStatusesLock.Lock()
    47  	defer s.podStatusesLock.Unlock()
    48  }
    49  
    50  func (s *statusManager_kubernetes10182) SetPodStatus() {
    51  	s.podStatusesLock.Lock()
    52  	defer s.podStatusesLock.Unlock()
    53  	s.podStatusChannel <- true
    54  }
    55  
    56  func NewStatusManager_kubernetes10182() *statusManager_kubernetes10182 {
    57  	return &statusManager_kubernetes10182{
    58  		podStatusChannel: make(chan bool),
    59  	}
    60  }
    61  
    62  // 	Example of deadlock trace:
    63  //
    64  //	G1 						G2							G3
    65  //	--------------------------------------------------------------------------------
    66  //	s.Start()
    67  //	s.syncBatch()
    68  //							s.SetPodStatus()
    69  //	<-s.podStatusChannel
    70  //							s.podStatusesLock.Lock()
    71  //							s.podStatusChannel <- true
    72  //							s.podStatusesLock.Unlock()
    73  //							return
    74  //	s.DeletePodStatus()
    75  //														s.podStatusesLock.Lock()
    76  //														s.podStatusChannel <- true
    77  //	s.podStatusesLock.Lock()
    78  //	-----------------------------------G1,G3 leak-------------------------------------
    79  
    80  func Kubernetes10182() {
    81  	prof := pprof.Lookup("goroutineleak")
    82  	defer func() {
    83  		time.Sleep(100 * time.Millisecond)
    84  		prof.WriteTo(os.Stdout, 2)
    85  	}()
    86  
    87  	for i := 0; i < 1000; i++ {
    88  		go func() {
    89  			s := NewStatusManager_kubernetes10182()
    90  			go s.Start()
    91  			go s.SetPodStatus()
    92  			go s.SetPodStatus()
    93  		}()
    94  	}
    95  }
    96  

View as plain text