leakcheck.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Copyright (c) 2018 gevent community
  2. # Copyright (c) 2021 greenlet community
  3. #
  4. # This was originally part of gevent's test suite. The main author
  5. # (Jason Madden) vendored a copy of it into greenlet.
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. from __future__ import print_function
  25. import os
  26. import sys
  27. import gc
  28. from functools import wraps
  29. import unittest
  30. import objgraph
  31. # graphviz 0.18 (Nov 7 2021), available only on Python 3.6 and newer,
  32. # has added type hints (sigh). It wants to use ``typing.Literal`` for
  33. # some stuff, but that's only available on Python 3.9+. If that's not
  34. # found, it creates a ``unittest.mock.MagicMock`` object and annotates
  35. # with that. These are GC'able objects, and doing almost *anything*
  36. # with them results in an explosion of objects. For example, trying to
  37. # compare them for equality creates new objects. This causes our
  38. # leakchecks to fail, with reports like:
  39. #
  40. # greenlet.tests.leakcheck.LeakCheckError: refcount increased by [337, 1333, 343, 430, 530, 643, 769]
  41. # _Call 1820 +546
  42. # dict 4094 +76
  43. # MagicProxy 585 +73
  44. # tuple 2693 +66
  45. # _CallList 24 +3
  46. # weakref 1441 +1
  47. # function 5996 +1
  48. # type 736 +1
  49. # cell 592 +1
  50. # MagicMock 8 +1
  51. #
  52. # To avoid this, we *could* filter this type of object out early. In
  53. # principle it could leak, but we don't use mocks in greenlet, so it
  54. # doesn't leak from us. However, a further issue is that ``MagicMock``
  55. # objects have subobjects that are also GC'able, like ``_Call``, and
  56. # those create new mocks of their own too. So we'd have to filter them
  57. # as well, and they're not public. That's OK, we can workaround the
  58. # problem by being very careful to never compare by equality or other
  59. # user-defined operators, only using object identity or other builtin
  60. # functions.
  61. RUNNING_ON_GITHUB_ACTIONS = os.environ.get('GITHUB_ACTIONS')
  62. RUNNING_ON_TRAVIS = os.environ.get('TRAVIS') or RUNNING_ON_GITHUB_ACTIONS
  63. RUNNING_ON_APPVEYOR = os.environ.get('APPVEYOR')
  64. RUNNING_ON_CI = RUNNING_ON_TRAVIS or RUNNING_ON_APPVEYOR
  65. RUNNING_ON_MANYLINUX = os.environ.get('GREENLET_MANYLINUX')
  66. SKIP_LEAKCHECKS = RUNNING_ON_MANYLINUX or os.environ.get('GREENLET_SKIP_LEAKCHECKS')
  67. SKIP_FAILING_LEAKCHECKS = os.environ.get('GREENLET_SKIP_FAILING_LEAKCHECKS')
  68. ONLY_FAILING_LEAKCHECKS = os.environ.get('GREENLET_ONLY_FAILING_LEAKCHECKS')
  69. def ignores_leakcheck(func):
  70. """
  71. Ignore the given object during leakchecks.
  72. Can be applied to a method, in which case the method will run, but
  73. will not be subject to leak checks.
  74. If applied to a class, the entire class will be skipped during leakchecks. This
  75. is intended to be used for classes that are very slow and cause problems such as
  76. test timeouts; typically it will be used for classes that are subclasses of a base
  77. class and specify variants of behaviour (such as pool sizes).
  78. """
  79. func.ignore_leakcheck = True
  80. return func
  81. def fails_leakcheck(func):
  82. """
  83. Mark that the function is known to leak.
  84. """
  85. func.fails_leakcheck = True
  86. if SKIP_FAILING_LEAKCHECKS:
  87. func = unittest.skip("Skipping known failures")(func)
  88. return func
  89. class LeakCheckError(AssertionError):
  90. pass
  91. if hasattr(sys, 'getobjects'):
  92. # In a Python build with ``--with-trace-refs``, make objgraph
  93. # trace *all* the objects, not just those that are tracked by the
  94. # GC
  95. class _MockGC(object):
  96. def get_objects(self):
  97. return sys.getobjects(0) # pylint:disable=no-member
  98. def __getattr__(self, name):
  99. return getattr(gc, name)
  100. objgraph.gc = _MockGC()
  101. fails_strict_leakcheck = fails_leakcheck
  102. else:
  103. def fails_strict_leakcheck(func):
  104. """
  105. Decorator for a function that is known to fail when running
  106. strict (``sys.getobjects()``) leakchecks.
  107. This type of leakcheck finds all objects, even those, such as
  108. strings, which are not tracked by the garbage collector.
  109. """
  110. return func
  111. class ignores_types_in_strict_leakcheck(object):
  112. def __init__(self, types):
  113. self.types = types
  114. def __call__(self, func):
  115. func.leakcheck_ignore_types = self.types
  116. return func
  117. class _RefCountChecker(object):
  118. # Some builtin things that we ignore
  119. # XXX: Those things were ignored by gevent, but they're important here,
  120. # presumably.
  121. IGNORED_TYPES = () #(tuple, dict, types.FrameType, types.TracebackType)
  122. def __init__(self, testcase, function):
  123. self.testcase = testcase
  124. self.function = function
  125. self.deltas = []
  126. self.peak_stats = {}
  127. self.ignored_types = ()
  128. # The very first time we are called, we have already been
  129. # self.setUp() by the test runner, so we don't need to do it again.
  130. self.needs_setUp = False
  131. def _include_object_p(self, obj):
  132. # pylint:disable=too-many-return-statements
  133. #
  134. # See the comment block at the top. We must be careful to
  135. # avoid invoking user-defined operations.
  136. if obj is self:
  137. return False
  138. kind = type(obj)
  139. # ``self._include_object_p == obj`` returns NotImplemented
  140. # for non-function objects, which causes the interpreter
  141. # to try to reverse the order of arguments...which leads
  142. # to the explosion of mock objects. We don't want that, so we implement
  143. # the check manually.
  144. if kind == type(self._include_object_p):
  145. try:
  146. # pylint:disable=not-callable
  147. exact_method_equals = self._include_object_p.__eq__(obj)
  148. except AttributeError:
  149. # Python 2.7 methods may only have __cmp__, and that raises a
  150. # TypeError for non-method arguments
  151. # pylint:disable=no-member
  152. exact_method_equals = self._include_object_p.__cmp__(obj) == 0
  153. if exact_method_equals is not NotImplemented and exact_method_equals:
  154. return False
  155. # Similarly, we need to check identity in our __dict__ to avoid mock explosions.
  156. for x in self.__dict__.values():
  157. if obj is x:
  158. return False
  159. if kind in self.ignored_types or kind in self.IGNORED_TYPES:
  160. return False
  161. return True
  162. def _growth(self):
  163. return objgraph.growth(limit=None, peak_stats=self.peak_stats,
  164. filter=self._include_object_p)
  165. def _report_diff(self, growth):
  166. if not growth:
  167. return "<Unable to calculate growth>"
  168. lines = []
  169. width = max(len(name) for name, _, _ in growth)
  170. for name, count, delta in growth:
  171. lines.append('%-*s%9d %+9d' % (width, name, count, delta))
  172. diff = '\n'.join(lines)
  173. return diff
  174. def _run_test(self, args, kwargs):
  175. gc_enabled = gc.isenabled()
  176. gc.disable()
  177. if self.needs_setUp:
  178. self.testcase.setUp()
  179. self.testcase.skipTearDown = False
  180. try:
  181. self.function(self.testcase, *args, **kwargs)
  182. finally:
  183. self.testcase.tearDown()
  184. self.testcase.doCleanups()
  185. self.testcase.skipTearDown = True
  186. self.needs_setUp = True
  187. if gc_enabled:
  188. gc.enable()
  189. def _growth_after(self):
  190. # Grab post snapshot
  191. if 'urlparse' in sys.modules:
  192. sys.modules['urlparse'].clear_cache()
  193. if 'urllib.parse' in sys.modules:
  194. sys.modules['urllib.parse'].clear_cache()
  195. return self._growth()
  196. def _check_deltas(self, growth):
  197. # Return false when we have decided there is no leak,
  198. # true if we should keep looping, raises an assertion
  199. # if we have decided there is a leak.
  200. deltas = self.deltas
  201. if not deltas:
  202. # We haven't run yet, no data, keep looping
  203. return True
  204. if gc.garbage:
  205. raise LeakCheckError("Generated uncollectable garbage %r" % (gc.garbage,))
  206. # the following configurations are classified as "no leak"
  207. # [0, 0]
  208. # [x, 0, 0]
  209. # [... a, b, c, d] where a+b+c+d = 0
  210. #
  211. # the following configurations are classified as "leak"
  212. # [... z, z, z] where z > 0
  213. if deltas[-2:] == [0, 0] and len(deltas) in (2, 3):
  214. return False
  215. if deltas[-3:] == [0, 0, 0]:
  216. return False
  217. if len(deltas) >= 4 and sum(deltas[-4:]) == 0:
  218. return False
  219. if len(deltas) >= 3 and deltas[-1] > 0 and deltas[-1] == deltas[-2] and deltas[-2] == deltas[-3]:
  220. diff = self._report_diff(growth)
  221. raise LeakCheckError('refcount increased by %r\n%s' % (deltas, diff))
  222. # OK, we don't know for sure yet. Let's search for more
  223. if sum(deltas[-3:]) <= 0 or sum(deltas[-4:]) <= 0 or deltas[-4:].count(0) >= 2:
  224. # this is suspicious, so give a few more runs
  225. limit = 11
  226. else:
  227. limit = 7
  228. if len(deltas) >= limit:
  229. raise LeakCheckError('refcount increased by %r\n%s'
  230. % (deltas,
  231. self._report_diff(growth)))
  232. # We couldn't decide yet, keep going
  233. return True
  234. def __call__(self, args, kwargs):
  235. for _ in range(3):
  236. gc.collect()
  237. expect_failure = getattr(self.function, 'fails_leakcheck', False)
  238. if expect_failure:
  239. self.testcase.expect_greenlet_leak = True
  240. self.ignored_types = getattr(self.function, "leakcheck_ignore_types", ())
  241. # Capture state before; the incremental will be
  242. # updated by each call to _growth_after
  243. growth = self._growth()
  244. try:
  245. while self._check_deltas(growth):
  246. self._run_test(args, kwargs)
  247. growth = self._growth_after()
  248. self.deltas.append(sum((stat[2] for stat in growth)))
  249. except LeakCheckError:
  250. if not expect_failure:
  251. raise
  252. else:
  253. if expect_failure:
  254. raise LeakCheckError("Expected %s to leak but it did not." % (self.function,))
  255. def wrap_refcount(method):
  256. if getattr(method, 'ignore_leakcheck', False) or SKIP_LEAKCHECKS:
  257. return method
  258. @wraps(method)
  259. def wrapper(self, *args, **kwargs): # pylint:disable=too-many-branches
  260. if getattr(self, 'ignore_leakcheck', False):
  261. raise unittest.SkipTest("This class ignored during leakchecks")
  262. if ONLY_FAILING_LEAKCHECKS and not getattr(method, 'fails_leakcheck', False):
  263. raise unittest.SkipTest("Only running tests that fail leakchecks.")
  264. return _RefCountChecker(self, method)(args, kwargs)
  265. return wrapper