25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
3.0 KiB

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