Skip to content

Infinite loop with Closing ConfigurationOption #869

Closed
@AndreyFrolov44

Description

@AndreyFrolov44

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)

Activity

Molandrious

Molandrious commented on Mar 10, 2025

@Molandrious

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

dev-petrov

dev-petrov commented on Mar 18, 2025

@dev-petrov

Release?

added theissue type on May 28, 2025
ZipFile

ZipFile commented on May 28, 2025

@ZipFile
Contributor

Fixed in v4.47.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @ZipFile@Molandrious@dev-petrov@AndreyFrolov44

      Issue actions

        Infinite loop with Closing ConfigurationOption · Issue #869 · ets-labs/python-dependency-injector