8000 Infinite loop with Closing ConfigurationOption · Issue #869 · ets-labs/python-dependency-injector · GitHub
[go: up one dir, main page]

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0