Skip to content

Test cases not garbage collected after run #56007

Closed

Description

@fabioz
mannequin
BPO 11798
Nosy @tim-one, @rhettinger, @terryjreedy, @vstinner, @fabioz, @benjaminp, @ezio-melotti, @bitdancer, @voidspace, @asvetlov, @meadori, @xdegaye, @charettes
PRs
  • bpo-30813: Fix unittest when hunting refleaks #2502
  • [3.6] bpo-30813: Fix unittest when hunting refleaks (#2502) #2505
  • [3.5] bpo-30813: Fix unittest when hunting refleaks (#2502) #2506
  • Files
  • 11798.patch
  • 11798-20130803-matthewlmcclure.patch
  • issue11798.diff
  • countTestCases.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/voidspace'
    closed_at = <Date 2014-09-21.08:15:26.276>
    created_at = <Date 2011-04-07.16:15:56.969>
    labels = ['type-bug', 'library']
    title = 'Test cases not garbage collected after run'
    updated_at = <Date 2017-06-30.16:52:46.736>
    user = 'https://github.com/fabioz'

    bugs.python.org fields:

    activity = <Date 2017-06-30.16:52:46.736>
    actor = 'gvanrossum'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2014-09-21.08:15:26.276>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2011-04-07.16:15:56.969>
    creator = 'fabioz'
    dependencies = []
    files = ['27352', '31154', '31483', '33131']
    hgrepos = []
    issue_num = 11798
    keywords = ['patch']
    message_count = 68.0
    messages = ['133225', '133226', '133227', '133228', '133229', '133231', '133232', '133236', '133237', '133239', '133241', '136961', '136967', '137464', '171623', '171837', '188424', '194228', '194268', '194398', '194430', '194432', '196273', '196275', '196281', '196282', '196297', '196300', '196301', '196402', '196404', '196406', '196431', '196433', '196607', '196618', '196659', '196665', '196701', '196702', '196703', '196705', '196742', '196766', '197712', '197713', '197714', '197734', '197735', '197888', '198060', '198062', '198066', '198068', '198069', '198071', '198072', '198076', '198080', '206177', '207046', '207047', '207057', '207070', '207081', '297374', '297384', '297388']
    nosy_count = 18.0
    nosy_names = ['tim.peters', 'rhettinger', 'terry.reedy', 'vstinner', 'fabioz', 'benjamin.peterson', 'ezio.melotti', 'r.david.murray', 'michael.foord', 'matthewlmcclure', 'asvetlov', 'meador.inge', 'xdegaye', 'tshepang', 'python-dev', 'tomwardill', 'matthewlmcclure-gmail', 'charettes']
    pr_nums = ['2502', '2505', '2506']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue11798'
    versions = ['Python 3.4']

    Activity

    1. fabioz commented on Apr 7, 2011

      fabiozmannequin
      MannequinAuthor

      Right now, when doing a test case, one must clear all the variables created in the test class, and I believe this shouldn't be needed...

      E.g.:

      class Test(TestCase):
        def setUp(self):
          self.obj1 = MyObject()

      ...

        def tearDown(self):
          del self.obj1

      Ideally (in my view), right after running the test, it should be garbage-collected and the explicit tearDown wouldn't be needed (as the test would garbage-collected, that reference would automatically die), because this is currently very error prone... (and probably a source of leaks for any sufficiently big test suite).

      If that's accepted, I can provide a patch.

    2. added
      stdlib
      type-bug
      on Apr 7, 2011
    3. benjaminp commented on Apr 7, 2011

      @benjaminp
      Contributor

      You don't have to clear them; you just have to finalize them. Anyway, this is essentially impossible to do in a backward compatible way given that TestCases are expected to stay around.

    4. exarkun commented on Apr 7, 2011

      exarkunmannequin
      Mannequin

      Trial lets test cases get garbaged collected. When we noticed this wasn't happening, we treated it as a bug and fixed it. No one ever complained about the change. I don't see any obvious way in which an application would even be able to tell the difference (a user can tell the difference by looking at top). In what case do you think this change would result in broken application code?

    5. fabioz commented on Apr 7, 2011

      fabiozmannequin
      MannequinAuthor

      I do get the idea of the backward incompatibility, although I think it's really minor in this case.

      Just for some data, the pydev test runner has had a fix to clear those test cases for quite a while already and no one has complained about it (it actually makes each of the tests None after run, so, if someone tries to access it after that, it would be pretty clear that it's not there anymore).

    6. benjaminp commented on Apr 7, 2011

      @benjaminp
      Contributor

      2011/4/7 Jean-Paul Calderone <report@bugs.python.org>:

      Jean-Paul Calderone <invalid@example.invalid> added the comment:

      Trial lets test cases get garbaged collected.  When we noticed this wasn't happening, we treated it as a bug and fixed it.  No one ever complained about the change.  I don't see any obvious way in which an application would even be able to tell the difference (a user can tell the difference by looking at top).  In what case do you think this change would result in broken application code?

      I thought unittest was just handed a bunch of TestCase instances and
      couldn't do much about insuring they were garbage collected.

    7. exarkun commented on Apr 7, 2011

      exarkunmannequin
      Mannequin

      I thought unittest was just handed a bunch of TestCase instances and couldn't do much about insuring they were garbage collected.

      True. But unittest could ensure that it doesn't keep a reference to each TestCase instance after it finishes running it. Then, if no one else has a reference either, it can be garbage collected.

    8. voidspace commented on Apr 7, 2011

      @voidspace
      Contributor

      A TestSuite (which is how tests are collected to run) holds all the tests and therefore keeps them all alive for the duration of the test run. (I presume this is the issue anyway.)

      How would you solve this - by having calling a TestSuite (which is how a test run is executed) remove members from themselves after each test execution? (Any failure tracebacks etc stored by the TestResult would also have to not keep the test alive.)

      My only concern would be backwards compatibility due to the change in behaviour.

    9. self-assigned this
      on Apr 7, 2011
    10. fabioz commented on Apr 7, 2011

      fabiozmannequin
      MannequinAuthor

      The current code I use in PyDev is below -- another option could be not adding the None to the list of tests, but removing it, but I find that a bit worse because in the current way if someone tries to access it after it's ran, it'll become clear it was removed.

      def run(self, result):
          for index, test in enumerate(self._tests):
              if result.shouldStop:
                  break
              test(result)
          # Let the memory be released! 
          self._tests[index] = None
      
          return result
          
          
      I think the issue with the test result storing the test is much more difficult to deal with (because currently most unit test frameworks probably rely on having it there), so, that's probably not something I'd try to fix as it'd probably break many clients... in which case it should be expected that the test is kept alive if it fails -- but as the idea is that all has to turn green anyways, I don't see this as a big issue :)
    11. exarkun commented on Apr 7, 2011

      exarkunmannequin
      Mannequin
    12. voidspace commented on Apr 7, 2011

      @voidspace
      Contributor

      Not keeping tests alive for the whole run seems like a good thing and either implementation seems fine to me. I'd be interested to hear if anyone else had any backwards compatibility concerns though.

    13. rhettinger commented on Apr 7, 2011

      @rhettinger
      Contributor

      Not keeping tests alive for the whole run seems like a
      good thing

      +1

      and either implementation seems fine to me.

      I slightly prefer Fabio;s assignment to None approach (for subtle reasons that I can't articulate at the moment).

    14. fabioz commented on May 26, 2011

      fabiozmannequin
      MannequinAuthor

      So Michal, it seems no objections were raised?

    15. 47 remaining items

    16. voidspace commented on Sep 19, 2013

      @voidspace
      Contributor

      Ah right, my mistake. Before setUp there shouldn't be test state. (Although tests are free to do whatever they want in __init__ too and I've seen plenty of TestCase subclasses using __init__ when they should be using setUp.)

      Essentially though _cleanup is a backwards compatibility feature - and suites that need _cleanup as a public api are already living without testcase dict cleanup.

    17. pitrou commented on Sep 19, 2013

      @pitrou
      Member

      That said, I agree that the __dict__ proposal is a hack, but as is the current _removetestAtIndex mechanism.

      The only clean solution I can think of would be to have two separate classes:

      • a TestSpec which contains execution-independent data about a test case, and knows how to instantiate it
      • a TestCase that is used for the actual test execution, but isn't saved in the test suite

      Maybe it's possible to do this without any backwards compat problem by making TestSuite.__iter__ always return TestCases (but freshly-created ones, from the inner test specs). The main point of adaptation would be TestLoader.loadTestsFromTestCase().

    18. voidspace commented on Sep 19, 2013

      @voidspace
      Contributor

      Having TestLoader.loadTestsFromTestCase() return a "lazy suite" that defers testcase instantiation until iteration is a nice idea.

      Unfortunately the TestSuite.addTests api iterates over a suite to add new tests. i.e. the code that builds a TestSuite for module probably already iterates over the suites returned by TestLoader.loadTestsFromTestCase - so the change would need to be more pervasive.

    19. pitrou commented on Sep 19, 2013

      @pitrou
      Member

      Unfortunately the TestSuite.addTests api iterates over a suite to add
      new tests. i.e. the code that builds a TestSuite for module probably
      already iterates over the suites returned by
      TestLoader.loadTestsFromTestCase - so the change would need to be
      more pervasive.

      addTests() could easily be tweaked to recognize that it gets passed a
      TestSuite, and special-case that.

      Also, TestCase objects could probably get an optional "spec" attribute
      pointing to their TestSpec.

    20. xdegaye commented on Dec 14, 2013

      xdegayemannequin
      Mannequin

      This seems to break BaseTestSuite.countTestCases when invoked after the TestSuite has been run:
      ...
      File "Lib/unittest/suite.py", line 42, in countTestCases
      cases += test.countTestCases()
      AttributeError: 'NoneType' object has no attribute 'countTestCases'

      Attached patch attempts to fix it.

    21. pitrou commented on Dec 28, 2013

      @pitrou
      Member

      No answer to Xavier's regression? The way this issue is being treated is a bit worrying.

    22. python-dev commented on Dec 28, 2013

      python-devmannequin
      Mannequin

      New changeset b668c409c10a by Antoine Pitrou in branch 'default':
      Fix breakage in TestSuite.countTestCases() introduced by issue bpo-11798.
      http://hg.python.org/cpython/rev/b668c409c10a

    23. voidspace commented on Dec 28, 2013

      @voidspace
      Contributor

      What's the purpose of _removed_tests in your fix, it doesn't appear to be used?

    24. pitrou commented on Dec 29, 2013

      @pitrou
      Member

      It is used, see countTestCases().

    25. voidspace commented on Dec 29, 2013

      @voidspace
      Contributor

      Ah yes, I see - sorry.

    26. vstinner commented on Jun 30, 2017

      @vstinner
      Member

      New changeset e4f9a2d by Victor Stinner in branch 'master':
      bpo-30813: Fix unittest when hunting refleaks (bpo-2502)
      e4f9a2d

    27. vstinner commented on Jun 30, 2017

      @vstinner
      Member

      New changeset 714afcc by Victor Stinner in branch '3.5':
      bpo-30813: Fix unittest when hunting refleaks (bpo-2502) (bpo-2506)
      714afcc

    28. vstinner commented on Jun 30, 2017

      @vstinner
      Member

      New changeset 22d4e8f by Victor Stinner in branch '3.6':
      bpo-30813: Fix unittest when hunting refleaks (bpo-2502) (bpo-2505)
      22d4e8f

    29. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions