Skip to content

Reference count variations after getting __dict__, getattr, and gc.collect #115822

Open
@encukou

Description

@encukou

The following program accesses __dict__ (!) and some attributes of two different classes, in a loop.
After some of the loops, the total reference count is exactly 2 less than usual. In this case, it's every 5th run of the outer loop:

import gc
import sys

class A:
    class_attr = None

class B:
    class_attr = None

a = A()
b = B()

for i in range(100):

    for obj in a, b:
        obj.__dict__
        for i in range(20):
            obj.class_attr

    gc.collect()
    print(sys.gettotalrefcount())
24189
24189
24187
24189
24189
24189
24189
24187
24189
24189
24189
24189
24187
...

The code is a highly simplified version running test_typing in refleak mode (-R:), where a and b correspond to typing.Tuple and typing.List. In the actual test, this happens with more classes, and more overlapping “periods”. Sometimes, the test doesn't meet our (currently rather generous) heuristic, and fails.

The behaviour is ultimately benign, but it interferes with reference leak testing. We should find out what's happening and make regrtest clear any caches and revert to a known state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions