modern-python / that-depends

DI-framework, inspired by python-dependency-injector, but without wiring. Python 3.12 is supported
https://that-depends.readthedocs.io/
MIT License
156 stars 12 forks source link

Teardown order of context resources #66

Closed stkrizh closed 2 months ago

stkrizh commented 2 months ago

Hello!

It seems like there is a similar issue (as in #62) with the teardown order, but this time for context resources.

from that_depends import BaseContainer, providers, container_context

async def resource(name, **kwargs):
    yield name
    print("tearing down", name)

class Container(BaseContainer):
    a = providers.AsyncContextResource(resource, "A")
    b = providers.AsyncContextResource(resource, "B", a=a)

async with container_context():
    await Container.a()
    await Container.b()

# Output:
# tearing down A
# tearing down B

Resource B depends on A, but A is torn down first. As far as I can tell, this can also cause runtime errors if during the teardown of resource B it's crucial resource A in the correct state.