Source file src/runtime/testdata/testgoroutineleakprofile/goker/cockroach13197.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/13197
     8   * Buggy version: fff27aedabafe20cef57f75905fe340cab48c2a4
     9   * fix commit-id: 9bf770cd8f6eaff5441b80d3aec1a5614e8747e1
    10   * Flaky: 100/100
    11   * Description: One goroutine executing (*Tx).awaitDone() blocks
    12   * waiting for a signal over context.Done() that never comes.
    13   */
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"os"
    19  	"runtime"
    20  	"runtime/pprof"
    21  )
    22  
    23  func init() {
    24  	register("Cockroach13197", Cockroach13197)
    25  }
    26  
    27  type DB_cockroach13197 struct{}
    28  
    29  func (db *DB_cockroach13197) begin(ctx context.Context) *Tx_cockroach13197 {
    30  	ctx, cancel := context.WithCancel(ctx)
    31  	tx := &Tx_cockroach13197{
    32  		cancel: cancel,
    33  		ctx:    ctx,
    34  	}
    35  	go tx.awaitDone() // G2
    36  	return tx
    37  }
    38  
    39  type Tx_cockroach13197 struct {
    40  	cancel context.CancelFunc
    41  	ctx    context.Context
    42  }
    43  
    44  func (tx *Tx_cockroach13197) awaitDone() {
    45  	<-tx.ctx.Done()
    46  }
    47  
    48  func (tx *Tx_cockroach13197) Rollback() {
    49  	tx.rollback()
    50  }
    51  
    52  func (tx *Tx_cockroach13197) rollback() {
    53  	tx.close()
    54  }
    55  
    56  func (tx *Tx_cockroach13197) close() {
    57  	tx.cancel()
    58  }
    59  
    60  // Example of goroutine leak trace:
    61  //
    62  // G1 				  G2
    63  //--------------------------------
    64  // begin()
    65  // .            awaitDone()
    66  // <<done>>     .
    67  //              <-tx.ctx.Done()
    68  //------------G2 leak-------------
    69  
    70  func Cockroach13197() {
    71  	prof := pprof.Lookup("goroutineleak")
    72  	defer func() {
    73  		// Yield several times to allow the child goroutine to run.
    74  		for i := 0; i < yieldCount; i++ {
    75  			runtime.Gosched()
    76  		}
    77  		prof.WriteTo(os.Stdout, 2)
    78  	}()
    79  
    80  	db := &DB_cockroach13197{}
    81  	db.begin(context.Background()) // G1
    82  }
    83  

View as plain text