Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes26980.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"
    10  	"runtime/pprof"
    11  	"sync"
    12  	"time"
    13  )
    14  
    15  func init() {
    16  	register("Kubernetes26980", Kubernetes26980)
    17  }
    18  
    19  type processorListener_kubernetes26980 struct {
    20  	lock sync.RWMutex
    21  	cond sync.Cond
    22  
    23  	pendingNotifications []interface{}
    24  }
    25  
    26  func (p *processorListener_kubernetes26980) add(notification interface{}) {
    27  	p.lock.Lock()
    28  	defer p.lock.Unlock()
    29  
    30  	p.pendingNotifications = append(p.pendingNotifications, notification)
    31  	p.cond.Broadcast()
    32  }
    33  
    34  func (p *processorListener_kubernetes26980) pop(stopCh <-chan struct{}) {
    35  	p.lock.Lock()
    36  	runtime.Gosched()
    37  	defer p.lock.Unlock()
    38  	for {
    39  		for len(p.pendingNotifications) == 0 {
    40  			select {
    41  			case <-stopCh:
    42  				return
    43  			default:
    44  			}
    45  			p.cond.Wait()
    46  		}
    47  		select {
    48  		case <-stopCh:
    49  			return
    50  		}
    51  	}
    52  }
    53  
    54  func newProcessListener_kubernetes26980() *processorListener_kubernetes26980 {
    55  	ret := &processorListener_kubernetes26980{
    56  		pendingNotifications: []interface{}{},
    57  	}
    58  	ret.cond.L = &ret.lock
    59  	return ret
    60  }
    61  func Kubernetes26980() {
    62  	prof := pprof.Lookup("goroutineleak")
    63  	defer func() {
    64  		time.Sleep(100 * time.Millisecond)
    65  		prof.WriteTo(os.Stdout, 2)
    66  	}()
    67  
    68  	for i := 0; i < 3000; i++ {
    69  		go func() {
    70  			pl := newProcessListener_kubernetes26980()
    71  			stopCh := make(chan struct{})
    72  			defer close(stopCh)
    73  			pl.add(1)
    74  			runtime.Gosched()
    75  			go pl.pop(stopCh)
    76  
    77  			resultCh := make(chan struct{})
    78  			go func() {
    79  				pl.lock.Lock()
    80  				close(resultCh)
    81  			}()
    82  			runtime.Gosched()
    83  			<-resultCh
    84  			pl.lock.Unlock()
    85  		}()
    86  	}
    87  }
    88  

View as plain text