Skip to content

Resource shutdown ignores dependencies between resources #432

@saulbein

Description

@saulbein

First of all, thank you for the great library.

I have encountered an issue with a bit of an unusual case of having dependencies between resources where both of them have cleanup, and the order of the cleanup matters. This is the container we are using:

class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    engine = providers.Singleton(...)

    session = providers.Resource(create_session, engine)
    token = providers.Resource(locked_token, session)

def create_session(engine):
    db_session = Session(engine)
    try:
        yield db_session
    finally:
        db_session.close()

def locked_token(session):
    token = session.query(...)
    session.commit()
    try:
        yield token
    finally:
        session.refresh(token)
        ...
        session.commit()

Resource initialization works completely fine, the session gets initialized before the token. But at shutdown of the application, we tried calling container.shutdown(), and what happened was that it closed the session first, thus making the token cleanup crash (since it got a new session - SQLAlchemy sessions create a new one on close()).

In other words, the change requested here is that the container shuts down resources in reverse order of starting them.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions