You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

119 line
3.0 KiB

  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. // Package leakcheck contains functions to check leaked goroutines.
  19. //
  20. // Call "defer leakcheck.Check(t)" at the beginning of tests.
  21. package leakcheck
  22. import (
  23. "runtime"
  24. "sort"
  25. "strings"
  26. "time"
  27. )
  28. var goroutinesToIgnore = []string{
  29. "testing.Main(",
  30. "testing.tRunner(",
  31. "testing.(*M).",
  32. "runtime.goexit",
  33. "created by runtime.gc",
  34. "created by runtime/trace.Start",
  35. "interestingGoroutines",
  36. "runtime.MHeap_Scavenger",
  37. "signal.signal_recv",
  38. "sigterm.handler",
  39. "runtime_mcall",
  40. "(*loggingT).flushDaemon",
  41. "goroutine in C code",
  42. }
  43. // RegisterIgnoreGoroutine appends s into the ignore goroutine list. The
  44. // goroutines whose stack trace contains s will not be identified as leaked
  45. // goroutines. Not thread-safe, only call this function in init().
  46. func RegisterIgnoreGoroutine(s string) {
  47. goroutinesToIgnore = append(goroutinesToIgnore, s)
  48. }
  49. func ignore(g string) bool {
  50. sl := strings.SplitN(g, "\n", 2)
  51. if len(sl) != 2 {
  52. return true
  53. }
  54. stack := strings.TrimSpace(sl[1])
  55. if strings.HasPrefix(stack, "testing.RunTests") {
  56. return true
  57. }
  58. if stack == "" {
  59. return true
  60. }
  61. for _, s := range goroutinesToIgnore {
  62. if strings.Contains(stack, s) {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. // interestingGoroutines returns all goroutines we care about for the purpose of
  69. // leak checking. It excludes testing or runtime ones.
  70. func interestingGoroutines() (gs []string) {
  71. buf := make([]byte, 2<<20)
  72. buf = buf[:runtime.Stack(buf, true)]
  73. for _, g := range strings.Split(string(buf), "\n\n") {
  74. if !ignore(g) {
  75. gs = append(gs, g)
  76. }
  77. }
  78. sort.Strings(gs)
  79. return
  80. }
  81. // Errorfer is the interface that wraps the Errorf method. It's a subset of
  82. // testing.TB to make it easy to use Check.
  83. type Errorfer interface {
  84. Errorf(format string, args ...interface{})
  85. }
  86. func check(efer Errorfer, timeout time.Duration) {
  87. // Loop, waiting for goroutines to shut down.
  88. // Wait up to timeout, but finish as quickly as possible.
  89. deadline := time.Now().Add(timeout)
  90. var leaked []string
  91. for time.Now().Before(deadline) {
  92. if leaked = interestingGoroutines(); len(leaked) == 0 {
  93. return
  94. }
  95. time.Sleep(50 * time.Millisecond)
  96. }
  97. for _, g := range leaked {
  98. efer.Errorf("Leaked goroutine: %v", g)
  99. }
  100. }
  101. // Check looks at the currently-running goroutines and checks if there are any
  102. // interestring (created by gRPC) goroutines leaked. It waits up to 10 seconds
  103. // in the error cases.
  104. func Check(efer Errorfer) {
  105. check(efer, 10*time.Second)
  106. }