Source file src/runtime/testdata/testgoroutineleakprofile/goker/grpc1424.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: grpc-go
     7   * Issue or PR  : https://github.com/grpc/grpc-go/pull/1424
     8   * Buggy version: 39c8c3866d926d95e11c03508bf83d00f2963f91
     9   * fix commit-id: 64bd0b04a7bb1982078bae6a2ab34c226125fbc1
    10   * Flaky: 100/100
    11   */
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"runtime/pprof"
    17  	"sync"
    18  	"time"
    19  )
    20  
    21  func init() {
    22  	register("Grpc1424", Grpc1424)
    23  }
    24  
    25  type Balancer_grpc1424 interface {
    26  	Notify() <-chan bool
    27  }
    28  
    29  type roundRobin_grpc1424 struct {
    30  	mu     sync.Mutex
    31  	addrCh chan bool
    32  }
    33  
    34  func (rr *roundRobin_grpc1424) Notify() <-chan bool {
    35  	return rr.addrCh
    36  }
    37  
    38  type addrConn_grpc1424 struct {
    39  	mu sync.Mutex
    40  }
    41  
    42  func (ac *addrConn_grpc1424) tearDown() {
    43  	ac.mu.Lock()
    44  	defer ac.mu.Unlock()
    45  }
    46  
    47  type dialOption_grpc1424 struct {
    48  	balancer Balancer_grpc1424
    49  }
    50  
    51  type ClientConn_grpc1424 struct {
    52  	dopts dialOption_grpc1424
    53  	conns []*addrConn_grpc1424
    54  }
    55  
    56  func (cc *ClientConn_grpc1424) lbWatcher(doneChan chan bool) {
    57  	for addr := range cc.dopts.balancer.Notify() {
    58  		if addr {
    59  			// nop, make compiler happy
    60  		}
    61  		var (
    62  			del []*addrConn_grpc1424
    63  		)
    64  		for _, a := range cc.conns {
    65  			del = append(del, a)
    66  		}
    67  		for _, c := range del {
    68  			c.tearDown()
    69  		}
    70  	}
    71  }
    72  
    73  func NewClientConn_grpc1424() *ClientConn_grpc1424 {
    74  	cc := &ClientConn_grpc1424{
    75  		dopts: dialOption_grpc1424{
    76  			&roundRobin_grpc1424{addrCh: make(chan bool)},
    77  		},
    78  	}
    79  	return cc
    80  }
    81  
    82  func DialContext_grpc1424() {
    83  	cc := NewClientConn_grpc1424()
    84  	waitC := make(chan error, 1)
    85  	go func() { // G2
    86  		defer close(waitC)
    87  		ch := cc.dopts.balancer.Notify()
    88  		if ch != nil {
    89  			doneChan := make(chan bool)
    90  			go cc.lbWatcher(doneChan) // G3
    91  			<-doneChan
    92  		}
    93  	}()
    94  	/// close addrCh
    95  	close(cc.dopts.balancer.(*roundRobin_grpc1424).addrCh)
    96  }
    97  
    98  func Grpc1424() {
    99  	prof := pprof.Lookup("goroutineleak")
   100  	defer func() {
   101  		time.Sleep(100 * time.Millisecond)
   102  		prof.WriteTo(os.Stdout, 2)
   103  	}()
   104  	go DialContext_grpc1424() // G1
   105  }
   106  

View as plain text