Source file src/runtime/testdata/testgoroutineleakprofile/goker/cockroach24808.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  	"context"
     9  	"os"
    10  	"runtime"
    11  	"runtime/pprof"
    12  	"sync"
    13  )
    14  
    15  func init() {
    16  	register("Cockroach24808", Cockroach24808)
    17  }
    18  
    19  type Compactor_cockroach24808 struct {
    20  	ch chan struct{}
    21  }
    22  
    23  type Stopper_cockroach24808 struct {
    24  	stop    sync.WaitGroup
    25  	stopper chan struct{}
    26  }
    27  
    28  func (s *Stopper_cockroach24808) RunWorker(ctx context.Context, f func(context.Context)) {
    29  	s.stop.Add(1)
    30  	go func() {
    31  		defer s.stop.Done()
    32  		f(ctx)
    33  	}()
    34  }
    35  
    36  func (s *Stopper_cockroach24808) ShouldStop() <-chan struct{} {
    37  	if s == nil {
    38  		return nil
    39  	}
    40  	return s.stopper
    41  }
    42  
    43  func (s *Stopper_cockroach24808) Stop() {
    44  	close(s.stopper)
    45  }
    46  
    47  func (c *Compactor_cockroach24808) Start(ctx context.Context, stopper *Stopper_cockroach24808) {
    48  	c.ch <- struct{}{}
    49  	stopper.RunWorker(ctx, func(ctx context.Context) {
    50  		for {
    51  			select {
    52  			case <-stopper.ShouldStop():
    53  				return
    54  			case <-c.ch:
    55  			}
    56  		}
    57  	})
    58  }
    59  
    60  func Cockroach24808() {
    61  	prof := pprof.Lookup("goroutineleak")
    62  	defer func() {
    63  		// Yield several times to allow the child goroutine to run.
    64  		for i := 0; i < yieldCount; i++ {
    65  			runtime.Gosched()
    66  		}
    67  		prof.WriteTo(os.Stdout, 2)
    68  	}()
    69  	go func() { // G1
    70  		stopper := &Stopper_cockroach24808{stopper: make(chan struct{})}
    71  		defer stopper.Stop()
    72  
    73  		compactor := &Compactor_cockroach24808{ch: make(chan struct{}, 1)}
    74  		compactor.ch <- struct{}{}
    75  
    76  		compactor.Start(context.Background(), stopper)
    77  	}()
    78  }
    79  

View as plain text