Source file src/runtime/testdata/testgoroutineleakprofile/goker/cockroach6181.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: cockroach
     7   * Issue or PR  : https://github.com/cockroachdb/cockroach/pull/6181
     8   * Buggy version: c0a232b5521565904b851699853bdbd0c670cf1e
     9   * fix commit-id: d5814e4886a776bf7789b3c51b31f5206480d184
    10   * Flaky: 57/100
    11   */
    12  package main
    13  
    14  import (
    15  	"io"
    16  	"os"
    17  	"runtime"
    18  	"runtime/pprof"
    19  	"sync"
    20  	"time"
    21  )
    22  
    23  func init() {
    24  	register("Cockroach6181", Cockroach6181)
    25  }
    26  
    27  type testDescriptorDB_cockroach6181 struct {
    28  	cache *rangeDescriptorCache_cockroach6181
    29  }
    30  
    31  func initTestDescriptorDB_cockroach6181() *testDescriptorDB_cockroach6181 {
    32  	return &testDescriptorDB_cockroach6181{&rangeDescriptorCache_cockroach6181{}}
    33  }
    34  
    35  type rangeDescriptorCache_cockroach6181 struct {
    36  	rangeCacheMu sync.RWMutex
    37  }
    38  
    39  func (rdc *rangeDescriptorCache_cockroach6181) LookupRangeDescriptor() {
    40  	rdc.rangeCacheMu.RLock()
    41  	runtime.Gosched()
    42  	io.Discard.Write([]byte(rdc.String()))
    43  	rdc.rangeCacheMu.RUnlock()
    44  	rdc.rangeCacheMu.Lock()
    45  	rdc.rangeCacheMu.Unlock()
    46  }
    47  
    48  func (rdc *rangeDescriptorCache_cockroach6181) String() string {
    49  	rdc.rangeCacheMu.RLock()
    50  	defer rdc.rangeCacheMu.RUnlock()
    51  	return rdc.stringLocked()
    52  }
    53  
    54  func (rdc *rangeDescriptorCache_cockroach6181) stringLocked() string {
    55  	return "something here"
    56  }
    57  
    58  func doLookupWithToken_cockroach6181(rc *rangeDescriptorCache_cockroach6181) {
    59  	rc.LookupRangeDescriptor()
    60  }
    61  
    62  func testRangeCacheCoalescedRequests_cockroach6181() {
    63  	db := initTestDescriptorDB_cockroach6181()
    64  	pauseLookupResumeAndAssert := func() {
    65  		var wg sync.WaitGroup
    66  		for i := 0; i < 3; i++ {
    67  			wg.Add(1)
    68  			go func() { // G2,G3,...
    69  				doLookupWithToken_cockroach6181(db.cache)
    70  				wg.Done()
    71  			}()
    72  		}
    73  		wg.Wait()
    74  	}
    75  	pauseLookupResumeAndAssert()
    76  }
    77  
    78  func Cockroach6181() {
    79  	prof := pprof.Lookup("goroutineleak")
    80  	defer func() {
    81  		time.Sleep(100 * time.Millisecond)
    82  		prof.WriteTo(os.Stdout, 2)
    83  	}()
    84  	for i := 0; i < 100; i++ {
    85  		go testRangeCacheCoalescedRequests_cockroach6181() // G1
    86  	}
    87  }
    88  

View as plain text