github.com/lzhfromustc/gofuzz@v0.0.0-20211116160056-151b3108bbd1/docker/builder/leakcheck.go (about)

     1  /*
     2   *
     3   * Copyright 2017 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package leakcheck contains functions to check leaked goroutines.
    20  //
    21  // Call "defer leakcheck.Check(t)" at the beginning of tests.
    22  package leakcheck
    23  
    24  import (
    25  	"runtime"
    26  	"sort"
    27  	"strings"
    28  	"time"
    29  )
    30  
    31  var goroutinesToIgnore = []string{
    32  	"testing.Main(",
    33  	"testing.tRunner(",
    34  	"testing.(*M).",
    35  	"runtime.goexit",
    36  	"created by runtime.gc",
    37  	"created by runtime/trace.Start",
    38  	"interestingGoroutines",
    39  	"runtime.MHeap_Scavenger",
    40  	"signal.signal_recv",
    41  	"sigterm.handler",
    42  	"runtime_mcall",
    43  	"(*loggingT).flushDaemon",
    44  	"goroutine in C code",
    45  	"httputil.DumpRequestOut", // TODO: Remove this once Go1.13 support is removed. https://github.com/golang/go/issues/37669.
    46  }
    47  
    48  // RegisterIgnoreGoroutine appends s into the ignore goroutine list. The
    49  // goroutines whose stack trace contains s will not be identified as leaked
    50  // goroutines. Not thread-safe, only call this function in init().
    51  func RegisterIgnoreGoroutine(s string) {
    52  	goroutinesToIgnore = append(goroutinesToIgnore, s)
    53  }
    54  
    55  func ignore(g string) bool {
    56  	sl := strings.SplitN(g, "\n", 2)
    57  	if len(sl) != 2 {
    58  		return true
    59  	}
    60  	stack := strings.TrimSpace(sl[1])
    61  	if strings.HasPrefix(stack, "testing.RunTests") {
    62  		return true
    63  	}
    64  
    65  	if stack == "" {
    66  		return true
    67  	}
    68  
    69  	for _, s := range goroutinesToIgnore {
    70  		if strings.Contains(stack, s) {
    71  			return true
    72  		}
    73  	}
    74  
    75  	return false
    76  }
    77  
    78  // interestingGoroutines returns all goroutines we care about for the purpose of
    79  // leak checking. It excludes testing or runtime ones.
    80  func interestingGoroutines() (gs []string) {
    81  	buf := make([]byte, 2<<20)
    82  	buf = buf[:runtime.Stack(buf, true)]
    83  	for _, g := range strings.Split(string(buf), "\n\n") {
    84  		if !ignore(g) {
    85  			gs = append(gs, g)
    86  		}
    87  	}
    88  	sort.Strings(gs)
    89  	return
    90  }
    91  
    92  // Errorfer is the interface that wraps the Errorf method. It's a subset of
    93  // testing.TB to make it easy to use Check.
    94  type Errorfer interface {
    95  	Errorf(format string, args ...interface{})
    96  }
    97  
    98  func check(efer Errorfer, timeout time.Duration) {
    99  	// gooracle: comment out for ignoring timeout sideaffect to benchmark
   100  
   101  	// Loop, waiting for goroutines to shut down.
   102  	// Wait up to timeout, but finish as quickly as possible.
   103  	// deadline := time.Now().Add(timeout)
   104  	// var leaked []string
   105  	// for time.Now().Before(deadline) {
   106  	// 	if leaked = interestingGoroutines(); len(leaked) == 0 {
   107  	// 		return
   108  	// 	}
   109  	// 	time.Sleep(50 * time.Millisecond)
   110  	// }
   111  	// for _, g := range leaked {
   112  	// 	efer.Errorf("Leaked goroutine: %v", g)
   113  	// }
   114  }
   115  
   116  // Check looks at the currently-running goroutines and checks if there are any
   117  // interesting (created by gRPC) goroutines leaked. It waits up to 10 seconds
   118  // in the error cases.
   119  func Check(efer Errorfer) {
   120  	check(efer, 10*time.Second)
   121  }