Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes5316.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/5316
     8   * Buggy version: c868b0bbf09128960bc7c4ada1a77347a464d876
     9   * fix commit-id: cc3a433a7abc89d2f766d4c87eaae9448e3dc091
    10   * Flaky: 100/100
    11   */
    12  
    13  package main
    14  
    15  import (
    16  	"errors"
    17  	"math/rand"
    18  	"os"
    19  	"runtime"
    20  	"runtime/pprof"
    21  	"time"
    22  )
    23  
    24  func init() {
    25  	register("Kubernetes5316", Kubernetes5316)
    26  }
    27  
    28  func finishRequest_kubernetes5316(timeout time.Duration, fn func() error) {
    29  	ch := make(chan bool)
    30  	errCh := make(chan error)
    31  	go func() { // G2
    32  		if err := fn(); err != nil {
    33  			errCh <- err
    34  		} else {
    35  			ch <- true
    36  		}
    37  	}()
    38  
    39  	select {
    40  	case <-ch:
    41  	case <-errCh:
    42  	case <-time.After(timeout):
    43  	}
    44  }
    45  
    46  func Kubernetes5316() {
    47  	prof := pprof.Lookup("goroutineleak")
    48  	defer func() {
    49  		// Wait a bit because the child goroutine relies on timed operations.
    50  		time.Sleep(100 * time.Millisecond)
    51  
    52  		// Yield several times to allow the child goroutine to run
    53  		for i := 0; i < yieldCount; i++ {
    54  			runtime.Gosched()
    55  		}
    56  		prof.WriteTo(os.Stdout, 2)
    57  	}()
    58  	go func() {
    59  		fn := func() error {
    60  			time.Sleep(2 * time.Millisecond)
    61  			if rand.Intn(10) > 5 {
    62  				return errors.New("Error")
    63  			}
    64  			return nil
    65  		}
    66  		go finishRequest_kubernetes5316(time.Microsecond, fn) // G1
    67  	}()
    68  }
    69  

View as plain text