Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of __self__ (possibly in conjunction with providers.Container) seems to cause memory leaks #790

Open
colonelpanic8 opened this issue Mar 2, 2024 · 0 comments

Comments

@colonelpanic8
Copy link

Here's a (pretty sloppy) sort of minimal reproduction, together with some investigation that I've been doing:

import gc
import time
import weakref

import objgraph
from dependency_injector import containers, providers


class Parent(containers.DeclarativeContainer):
    __self__ = providers.Self()
    config = providers.Configuration(default={})

    a_factory = providers.Factory(lambda container: True, __self__)


class Child(containers.DeclarativeContainer):
    a_parent = providers.Container(Parent)


def test_parent_container_allocation(testing_container):
    parent = Parent()
    count = 500
    for _ in range(count):
        Child(a_parent=parent)
    time.sleep(1)
    gc.collect()

    random_container = weakref.ref(objgraph.by_type("DynamicContainer"))
    print(objgraph.show_backrefs(random_container(), max_depth=20))
    chain = objgraph.find_backref_chain(random_container, objgraph.is_proper_module)
    import ipdb

    ipdb.set_trace()
    assert len(objgraph.by_type("DynamicContainer")) < 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant