Resource shutdown ignores dependencies between resources
See original GitHub issueFirst 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:
- Created 2 years ago
- Comments:9 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
No worries, we all make mistakes sometimes 😃 Thanks for the fix!