question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resource shutdown ignores dependencies between resources

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
saulbeincommented, Jul 12, 2021

Hi again @rmk135,

The workaround has been fine for a while now.

We have a different framework that is responsible for application lifetime (Prefect) and we had the container integrated with it’s tasks, so that we can easily provide external dependencies. Since we have multiple projects using this setup, I would like to factor out the code linking these two frameworks, but I can’t do that unless container.shutdown() respects the shutdown order - the linker code can’t know about what the container has, just that it is a container.

Not trying to rush you or anything, just expanding on the use case.

0reactions
saulbeincommented, Jul 26, 2021

No worries, we all make mistakes sometimes 😃 Thanks for the fix!

Read more comments on GitHub >

github_iconTop Results From Across the Web

If Resources Do Not Stop - IBM
Supporting resource cannot stop: The supporting resources cannot be brought into the condition fulfilling the stop dependency, so the dependent resource that ...
Read more >
Manage Resource Lifecycle | Terraform - HashiCorp Developer
Update your configuration with lifecycle management blocks to prevent resource deletion, create resources before destroying them, and ignore changes to ...
Read more >
CWE-404: Improper Resource Shutdown or Release
Improper release or shutdown of resources can be resultant from improper error handling or insufficient resource tracking.
Read more >
Making Applications Highly Available ... - Oracle Help Center
When an application, process, or server fails in a cluster, you want the disruption to be as short as possible, if not completely...
Read more >
boot.pod — boot/worker 2.8.3 - cljdoc
data env pod-id pods shutdown-hooks worker-pod ... classloader-resources copy-resource dependency-loaded? get-classpath modifiable-classloader?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found