leakcheck.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. # Names of types that should be ignored. Use this when we cannot
  123. # or don't want to import the class directly.
  124. IGNORED_TYPE_NAMES = (
  125. # This appears in Python3.14 with the JIT enabled. It
  126. # doesn't seem to be directly exposed to Python; the only way to get
  127. # one is to cause code to get jitted and then look for all objects
  128. # and find one with this name. But they multiply as code
  129. # executes and gets jitted, in ways we don't want to rely on.
  130. # So just ignore it.
  131. 'uop_executor',
  132. )
  133. def __init__(self, testcase, function):
  134. self.testcase = testcase
  135. self.function = function
  136. self.deltas = []
  137. self.peak_stats = {}
  138. self.ignored_types = ()
  139. # The very first time we are called, we have already been
  140. # self.setUp() by the test runner, so we don't need to do it again.
  141. self.needs_setUp = False
  142. def _include_object_p(self, obj):
  143. # pylint:disable=too-many-return-statements
  144. #
  145. # See the comment block at the top. We must be careful to
  146. # avoid invoking user-defined operations.
  147. if obj is self:
  148. return False
  149. kind = type(obj)
  150. # ``self._include_object_p == obj`` returns NotImplemented
  151. # for non-function objects, which causes the interpreter
  152. # to try to reverse the order of arguments...which leads
  153. # to the explosion of mock objects. We don't want that, so we implement
  154. # the check manually.
  155. if kind == type(self._include_object_p):
  156. try:
  157. # pylint:disable=not-callable
  158. exact_method_equals = self._include_object_p.__eq__(obj)
  159. except AttributeError:
  160. # Python 2.7 methods may only have __cmp__, and that raises a
  161. # TypeError for non-method arguments
  162. # pylint:disable=no-member
  163. exact_method_equals = self._include_object_p.__cmp__(obj) == 0
  164. if exact_method_equals is not NotImplemented and exact_method_equals:
  165. return False
  166. # Similarly, we need to check identity in our __dict__ to avoid mock explosions.
  167. for x in self.__dict__.values():
  168. if obj is x:
  169. return False
  170. if (
  171. kind in self.ignored_types
  172. or kind in self.IGNORED_TYPES
  173. or kind.__name__ in self.IGNORED_TYPE_NAMES
  174. ):
  175. return False
  176. return True
  177. def _growth(self):
  178. return objgraph.growth(limit=None, peak_stats=self.peak_stats,
  179. filter=self._include_object_p)
  180. def _report_diff(self, growth):
  181. if not growth:
  182. return "<Unable to calculate growth>"
  183. lines = []
  184. width = max(len(name) for name, _, _ in growth)
  185. for name, count, delta in growth:
  186. lines.append('%-*s%9d %+9d' % (width, name, count, delta))
  187. diff = '\n'.join(lines)
  188. return diff
  189. def _run_test(self, args, kwargs):
  190. gc_enabled = gc.isenabled()
  191. gc.disable()
  192. if self.needs_setUp:
  193. self.testcase.setUp()
  194. self.testcase.skipTearDown = False
  195. try:
  196. self.function(self.testcase, *args, **kwargs)
  197. finally:
  198. self.testcase.tearDown()
  199. self.testcase.doCleanups()
  200. self.testcase.skipTearDown = True
  201. self.needs_setUp = True
  202. if gc_enabled:
  203. gc.enable()
  204. def _growth_after(self):
  205. # Grab post snapshot
  206. # pylint:disable=no-member
  207. if 'urlparse' in sys.modules:
  208. sys.modules['urlparse'].clear_cache()
  209. if 'urllib.parse' in sys.modules:
  210. sys.modules['urllib.parse'].clear_cache()
  211. return self._growth()
  212. def _check_deltas(self, growth):
  213. # Return false when we have decided there is no leak,
  214. # true if we should keep looping, raises an assertion
  215. # if we have decided there is a leak.
  216. deltas = self.deltas
  217. if not deltas:
  218. # We haven't run yet, no data, keep looping
  219. return True
  220. if gc.garbage:
  221. raise LeakCheckError("Generated uncollectable garbage %r" % (gc.garbage,))
  222. # the following configurations are classified as "no leak"
  223. # [0, 0]
  224. # [x, 0, 0]
  225. # [... a, b, c, d] where a+b+c+d = 0
  226. #
  227. # the following configurations are classified as "leak"
  228. # [... z, z, z] where z > 0
  229. if deltas[-2:] == [0, 0] and len(deltas) in (2, 3):
  230. return False
  231. if deltas[-3:] == [0, 0, 0]:
  232. return False
  233. if len(deltas) >= 4 and sum(deltas[-4:]) == 0:
  234. return False
  235. if len(deltas) >= 3 and deltas[-1] > 0 and deltas[-1] == deltas[-2] and deltas[-2] == deltas[-3]:
  236. diff = self._report_diff(growth)
  237. raise LeakCheckError('refcount increased by %r\n%s' % (deltas, diff))
  238. # OK, we don't know for sure yet. Let's search for more
  239. if sum(deltas[-3:]) <= 0 or sum(deltas[-4:]) <= 0 or deltas[-4:].count(0) >= 2:
  240. # this is suspicious, so give a few more runs
  241. limit = 11
  242. else:
  243. limit = 7
  244. if len(deltas) >= limit:
  245. raise LeakCheckError('refcount increased by %r\n%s'
  246. % (deltas,
  247. self._report_diff(growth)))
  248. # We couldn't decide yet, keep going
  249. return True
  250. def __call__(self, args, kwargs):
  251. for _ in range(3):
  252. gc.collect()
  253. expect_failure = getattr(self.function, 'fails_leakcheck', False)
  254. if expect_failure:
  255. self.testcase.expect_greenlet_leak = True
  256. self.ignored_types = getattr(self.function, "leakcheck_ignore_types", ())
  257. # Capture state before; the incremental will be
  258. # updated by each call to _growth_after
  259. growth = self._growth()
  260. try:
  261. while self._check_deltas(growth):
  262. self._run_test(args, kwargs)
  263. growth = self._growth_after()
  264. self.deltas.append(sum((stat[2] for stat in growth)))
  265. except LeakCheckError:
  266. if not expect_failure:
  267. raise
  268. else:
  269. if expect_failure:
  270. raise LeakCheckError("Expected %s to leak but it did not." % (self.function,))
  271. def wrap_refcount(method):
  272. if getattr(method, 'ignore_leakcheck', False) or SKIP_LEAKCHECKS:
  273. return method
  274. @wraps(method)
  275. def wrapper(self, *args, **kwargs): # pylint:disable=too-many-branches
  276. if getattr(self, 'ignore_leakcheck', False):
  277. raise unittest.SkipTest("This class ignored during leakchecks")
  278. if ONLY_FAILING_LEAKCHECKS and not getattr(method, 'fails_leakcheck', False):
  279. raise unittest.SkipTest("Only running tests that fail leakchecks.")
  280. return _RefCountChecker(self, method)(args, kwargs)
  281. return wrapper