Source file src/runtime/testdata/testgoroutineleakprofile/goker/moby33781.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: moby
     7   * Issue or PR  : https://github.com/moby/moby/pull/33781
     8   * Buggy version: 33fd3817b0f5ca4b87f0a75c2bd583b4425d392b
     9   * fix commit-id: 67297ba0051d39be544009ba76abea14bc0be8a4
    10   * Flaky: 25/100
    11   */
    12  
    13  package main
    14  
    15  import (
    16  	"context"
    17  	"os"
    18  	"runtime/pprof"
    19  	"time"
    20  )
    21  
    22  func init() {
    23  	register("Moby33781", Moby33781)
    24  }
    25  
    26  func monitor_moby33781(stop chan bool) {
    27  	probeInterval := time.Millisecond
    28  	probeTimeout := time.Millisecond
    29  	for {
    30  		select {
    31  		case <-stop:
    32  			return
    33  		case <-time.After(probeInterval):
    34  			results := make(chan bool)
    35  			ctx, cancelProbe := context.WithTimeout(context.Background(), probeTimeout)
    36  			go func() { // G3
    37  				results <- true
    38  				close(results)
    39  			}()
    40  			select {
    41  			case <-stop:
    42  				// results should be drained here
    43  				cancelProbe()
    44  				return
    45  			case <-results:
    46  				cancelProbe()
    47  			case <-ctx.Done():
    48  				cancelProbe()
    49  				<-results
    50  			}
    51  		}
    52  	}
    53  }
    54  
    55  func Moby33781() {
    56  	prof := pprof.Lookup("goroutineleak")
    57  	defer func() {
    58  		time.Sleep(100 * time.Millisecond)
    59  		prof.WriteTo(os.Stdout, 2)
    60  	}()
    61  	for i := 0; i < 100; i++ {
    62  		go func(i int) {
    63  			stop := make(chan bool)
    64  			go monitor_moby33781(stop) // G1
    65  			go func() {                // G2
    66  				time.Sleep(time.Duration(i) * time.Millisecond)
    67  				stop <- true
    68  			}()
    69  		}(i)
    70  	}
    71  }
    72  

View as plain text