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

Infinite loop with Closing ConfigurationOption #869

Open
AndreyFrolov44 opened this issue Mar 10, 2025 · 2 comments
Open

Infinite loop with Closing ConfigurationOption #869

AndreyFrolov44 opened this issue Mar 10, 2025 · 2 comments

Comments

@AndreyFrolov44
Copy link

After updating to 4.46.0, there is an endless iteration through the list when using Configuration.

For example:

from dependency_injector.containers import DeclarativeContainer
from dependency_injector.providers import Configuration, Resource, Factory
from dependency_injector.wiring import Provide, Closing


def test_resource(string: str):
    print("init resource")
    yield f"resource {string}"
    print("shutdown resource")


class Service:
    def __init__(self, resource: str):
        self.resource = resource

    def execute(self):
        print(self.resource)


class DIContainer(DeclarativeContainer):
    config = Configuration()
    resource = Resource(test_resource, config.foo)
    service = Factory(Service, resource=resource)


container = DIContainer()
container.config.from_dict({"foo": "test"})


def foo(
    service: Service = Closing[Provide["service"]],
):
    print("foo")
    service.execute()


if __name__ == "__main__":
    container.wire(modules=[__name__])

    foo()

At the same time, nothing is output to the console and no error occurs.

I suggest making the following fixes, but I'm not sure they won't break anything.

def _locate_dependent_closing_args(
    provider: providers.Provider, closing_deps: Dict[str, providers.Provider]
) -> Dict[str, providers.Provider]:
    for arg in [
        *getattr(provider, "args", []),
        *getattr(provider, "kwargs", {}).values(),
    ]:
        if not isinstance(arg, providers.Provider):
            continue
        if isinstance(arg, providers.ConfigurationOption):
            continue
        if isinstance(arg, providers.Resource):
            closing_deps[str(id(arg))] = arg

        _locate_dependent_closing_args(arg, closing_deps)
@Molandrious
Copy link

Got the same issue using Closing in FastApi handler (v0.115.11) + SQLAlchemy app (di v 4.46.0)

@dev-petrov
Copy link

Release?

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

3 participants