Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes11298.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("Kubernetes11298", Kubernetes11298)
    16  }
    17  
    18  type Signal_kubernetes11298 <-chan struct{}
    19  
    20  func After_kubernetes11298(f func()) Signal_kubernetes11298 {
    21  	ch := make(chan struct{})
    22  	go func() {
    23  		defer close(ch)
    24  		if f != nil {
    25  			f()
    26  		}
    27  	}()
    28  	return Signal_kubernetes11298(ch)
    29  }
    30  
    31  func Until_kubernetes11298(f func(), period time.Duration, stopCh <-chan struct{}) {
    32  	if f == nil {
    33  		return
    34  	}
    35  	for {
    36  		select {
    37  		case <-stopCh:
    38  			return
    39  		default:
    40  		}
    41  		f()
    42  		select {
    43  		case <-stopCh:
    44  		case <-time.After(period):
    45  		}
    46  	}
    47  
    48  }
    49  
    50  type notifier_kubernetes11298 struct {
    51  	lock sync.Mutex
    52  	cond *sync.Cond
    53  }
    54  
    55  // abort will be closed no matter what
    56  func (n *notifier_kubernetes11298) serviceLoop(abort <-chan struct{}) {
    57  	n.lock.Lock()
    58  	defer n.lock.Unlock()
    59  	for {
    60  		select {
    61  		case <-abort:
    62  			return
    63  		default:
    64  			ch := After_kubernetes11298(func() {
    65  				n.cond.Wait()
    66  			})
    67  			select {
    68  			case <-abort:
    69  				n.cond.Signal()
    70  				<-ch
    71  				return
    72  			case <-ch:
    73  			}
    74  		}
    75  	}
    76  }
    77  
    78  // abort will be closed no matter what
    79  func Notify_kubernetes11298(abort <-chan struct{}) {
    80  	n := &notifier_kubernetes11298{}
    81  	n.cond = sync.NewCond(&n.lock)
    82  	finished := After_kubernetes11298(func() {
    83  		Until_kubernetes11298(func() {
    84  			for {
    85  				select {
    86  				case <-abort:
    87  					return
    88  				default:
    89  					func() {
    90  						n.lock.Lock()
    91  						defer n.lock.Unlock()
    92  						n.cond.Signal()
    93  					}()
    94  				}
    95  			}
    96  		}, 0, abort)
    97  	})
    98  	Until_kubernetes11298(func() { n.serviceLoop(finished) }, 0, abort)
    99  }
   100  func Kubernetes11298() {
   101  	prof := pprof.Lookup("goroutineleak")
   102  	defer func() {
   103  		time.Sleep(100 * time.Millisecond)
   104  		prof.WriteTo(os.Stdout, 2)
   105  	}()
   106  
   107  	for i := 0; i < 1000; i++ {
   108  		go func() {
   109  			done := make(chan struct{})
   110  			notifyDone := After_kubernetes11298(func() { Notify_kubernetes11298(done) })
   111  			go func() {
   112  				defer close(done)
   113  				time.Sleep(300 * time.Nanosecond)
   114  			}()
   115  			<-notifyDone
   116  		}()
   117  	}
   118  }
   119  

View as plain text